Add another delay in the AMSv2 sd controller driver as a work-around for SD initialis...
[maemo-rb.git] / apps / plugins / lrcplayer.c
blob8e9878f78b07304a48479b5ec8431d6cc047ae7d
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>
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 = 0x200,
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 #ifdef HAVE_LCD_BITMAP
419 static bool isbrchr(const unsigned char *str, int len)
421 const unsigned char *p = "!,-.:;? 、。!,.:;?―";
422 if (isspace(*str))
423 return true;
425 while(*p)
427 int n = rb->utf8seek(p, 1);
428 if (len == n && !rb->strncmp(p, str, len))
429 return true;
430 p += n;
432 return false;
434 #endif
436 /* calculate how many lines is needed to display and store it.
437 * create cache if there is enough space in lrc_buffer. */
438 static struct lrc_brpos *calc_brpos(struct lrc_line *lrc_line, int i)
440 struct lrc_brpos *lrc_brpos;
441 struct lrc_word *lrc_word;
442 int nlrcbrpos = 0, max_lrcbrpos;
443 #ifdef HAVE_LCD_BITMAP
444 struct font* pf = rb->font_get(FONT_UI);
445 unsigned short ch;
446 #endif
447 struct snap {
448 int count, width;
449 int nword;
450 int word_count, word_width;
451 const unsigned char *str;
452 } sp, cr;
454 lrc_buffer_used = (lrc_buffer_used+3)&~3; /* 4 bytes aligned */
455 lrc_brpos = (struct lrc_brpos *) &lrc_buffer[lrc_buffer_used];
456 max_lrcbrpos = (lrc_buffer_end-lrc_buffer_used) / sizeof(struct lrc_brpos);
458 if (!lrc_line)
460 /* calc info for all lrcs and store them if possible */
461 size_t buffer_used = lrc_buffer_used;
462 bool too_many_lines = false;
463 current.too_many_lines = true;
464 for (lrc_line = current.ll_head; lrc_line; lrc_line = lrc_line->next)
466 FOR_NB_SCREENS(i)
468 lrc_brpos = calc_brpos(lrc_line, i);
469 if (!too_many_lines)
471 lrc_buffer_used += lrc_line->nline[i]*sizeof(struct lrc_brpos);
472 if (nlrcbrpos + lrc_line->nline[i] >= max_lrcbrpos)
474 too_many_lines = true;
475 lrc_buffer_used = buffer_used;
476 calc_brpos(lrc_line, i);
479 nlrcbrpos += lrc_line->nline[i];
482 current.too_many_lines = too_many_lines;
483 lrc_buffer_used = buffer_used;
484 current.nlrcbrpos = nlrcbrpos;
485 return NULL;
488 if (!current.too_many_lines)
490 /* use stored infos. */
491 struct lrc_line *temp_lrc = current.ll_head;
492 for (; temp_lrc != lrc_line; temp_lrc = temp_lrc->next)
494 lrc_brpos += temp_lrc->nline[SCREEN_MAIN];
495 #ifdef HAVE_REMOTE_LCD
496 lrc_brpos += temp_lrc->nline[SCREEN_REMOTE];
497 #endif
499 #if NB_SCREENS >= 2
500 while (i)
501 lrc_brpos += lrc_line->nline[--i];
502 #endif
503 return lrc_brpos;
506 /* calculate number of lines, line width and char count for each line. */
507 lrc_line->width = 0;
508 cr.nword = lrc_line->nword;
509 lrc_word = lrc_line->words+cr.nword;
510 cr.str = (lrc_word-1)->word;
511 sp.word_count = 0;
512 sp.word_width = 0;
513 sp.nword = 0;
514 sp.count = 0;
515 sp.width = 0;
516 do {
517 cr.count = 0;
518 cr.width = 0;
519 sp.str = NULL;
521 while (1)
523 while(cr.nword > 0 && cr.str >= (lrc_word-1)->word)
525 cr.nword--;
526 lrc_word--;
527 lrc_word->count = 0;
528 lrc_word->width = 0;
530 if (*cr.str == 0 || *cr.str == '\n')
531 break;
533 int c, w;
534 #ifdef HAVE_LCD_CHARCELLS
535 c = rb->utf8seek(cr.str, 1);
536 w = 1;
537 #else
538 c = ((long)rb->utf8decode(cr.str, &ch) - (long)cr.str);
539 if (rb->is_diacritic(ch, NULL))
540 w = 0;
541 else
542 w = rb->font_get_width(pf, ch);
543 if (cr.count && prefs.wrap && isbrchr(cr.str, c))
545 /* remember position of last space */
546 rb->memcpy(&sp, &cr, sizeof(struct snap));
547 sp.word_count = lrc_word->count;
548 sp.word_width = lrc_word->width;
549 if (!isspace(*cr.str) && cr.width+w <= vp_lyrics[i].width)
551 sp.count += c;
552 sp.width += w;
553 sp.word_count += c;
554 sp.word_width += w;
555 sp.str += c;
558 if (cr.count && cr.width+w > vp_lyrics[i].width)
560 if (sp.str != NULL) /* wrap */
562 rb->memcpy(&cr, &sp, sizeof(struct snap));
563 lrc_word = lrc_line->words+cr.nword;
564 lrc_word->count = sp.word_count;
565 lrc_word->width = sp.word_width;
567 break;
569 #endif
570 cr.count += c;
571 cr.width += w;
572 lrc_word->count += c;
573 lrc_word->width += w;
574 cr.str += c;
576 lrc_line->width += cr.width;
577 lrc_brpos->count = cr.count;
578 lrc_brpos->width = cr.width;
579 nlrcbrpos++;
580 lrc_brpos++;
581 cr.str = lrc_skip_space(cr.str);
582 } while (*cr.str && nlrcbrpos < max_lrcbrpos);
583 lrc_line->nline[i] = nlrcbrpos;
585 while (cr.nword > 0)
587 cr.nword--;
588 lrc_word--;
589 lrc_word->count = 0;
590 lrc_word->width = 0;
592 return lrc_brpos-nlrcbrpos;
595 /* sort lyrics by time using stable sort. */
596 static void sort_lrcs(void)
598 struct lrc_line *p = current.ll_head, **q = NULL, *t;
599 long time_max = 0;
601 current.ll_head = NULL;
602 current.ll_tail = &current.ll_head;
603 while (p != NULL)
605 t = p->next;
606 /* remove problematic lrc_lines.
607 * it would cause problem in display_lrc_line() if nword is 0. */
608 if (p->nword)
610 q = p->time_start >= time_max? current.ll_tail: &current.ll_head;
611 while ((*q) && (*q)->time_start <= p->time_start)
612 q = &((*q)->next);
613 p->next = *q;
614 *q = p;
615 if (!p->next)
617 time_max = p->time_start;
618 current.ll_tail = &p->next;
621 p = t;
623 if (!current.too_many_lines)
624 calc_brpos(NULL, 0); /* stored data depends on order of lrcs if exist */
626 static void init_time_tag(void)
628 struct lrc_line *lrc_line = current.ll_head;
629 int nline = 0;
630 if (current.type == TXT || current.type == ID3_USLT)
632 /* set time tag according to length of audio and total line count
633 * for not synched lyrics, so that scroll speed is almost constant. */
634 for (; lrc_line; lrc_line = lrc_line->next)
636 lrc_line->time_start = nline * current.length / current.nlrcbrpos;
637 lrc_line->time_start -= lrc_line->time_start%10;
638 lrc_line->old_time_start = -1;
639 nline += lrc_line->nline[SCREEN_MAIN];
640 #ifdef HAVE_REMOTE_LCD
641 nline += lrc_line->nline[SCREEN_REMOTE];
642 #endif
645 else
647 /* reset timetags to the value read from file */
648 for (; lrc_line; lrc_line = lrc_line->next)
650 lrc_line->time_start = lrc_line->old_time_start;
652 sort_lrcs();
654 current.changed_lrc = false;
657 /*******************************
658 * Serch lrc file.
659 *******************************/
661 /* search in same or parent directries of playing file.
662 * assume playing file is /aaa/bbb/ccc/ddd.mp3,
663 * this function searchs lrc file following order.
664 * /aaa/bbb/ccc/ddd.lrc
665 * /aaa/bbb/ddd.lrc
666 * /aaa/ddd.lrc
667 * /ddd.lrc
670 /* taken from apps/recorder/albumart.c */
671 static void fix_filename(char* name)
673 static const char invalid_chars[] = "*/:<>?\\|";
675 while (1)
677 if (*name == 0)
678 return;
679 if (*name == '"')
680 *name = '\'';
681 else if (rb->strchr(invalid_chars, *name))
682 *name = '_';
683 name++;
686 static bool find_lrc_file_helper(const char *base_dir)
688 char fname[MAX_PATH];
689 char *names[3] = {NULL, NULL, NULL};
690 char *p, *dir;
691 int i, len;
692 /* /aaa/bbb/ccc/ddd.mp3
693 * dir <--q names[0]
696 /* assuming file name starts with '/' */
697 rb->strcpy(temp_buf, current.mp3_file);
698 /* get file name and remove extension */
699 names[0] = rb->strrchr(temp_buf, '/')+1;
700 if ((p = rb->strrchr(names[0], '.')) != NULL)
701 *p = 0;
702 if (current.id3->title && rb->strcmp(names[0], current.id3->title))
704 rb->strlcpy(fname, current.id3->title, sizeof(fname));
705 fix_filename(fname);
706 names[1] = fname;
709 dir = temp_buf;
710 p = names[0]-1;
711 do {
712 int n;
713 *p = 0;
714 for (n = 0; ; n++)
716 if (n == 0)
718 len = rb->snprintf(current.lrc_file, MAX_PATH, "%s%s/",
719 base_dir, dir);
721 else if (n == 1)
723 /* check file in subfolder named prefs.lrc_directory
724 * in the directory of mp3 file. */
725 if (prefs.lrc_directory[0] == '/')
727 len = rb->snprintf(current.lrc_file, MAX_PATH, "%s%s/",
728 dir, prefs.lrc_directory);
730 else
731 continue;
733 else
734 break;
735 DEBUGF("check file in %s\n", current.lrc_file);
736 if (!rb->dir_exists(current.lrc_file))
737 continue;
738 for (current.type = 0; current.type < NUM_TYPES; current.type++)
740 for (i = 0; names[i] != NULL; i++)
742 rb->snprintf(&current.lrc_file[len], MAX_PATH-len,
743 "%s%s", names[i], extentions[current.type]);
744 if (rb->file_exists(current.lrc_file))
746 DEBUGF("found: `%s'\n", current.lrc_file);
747 return true;
752 } while ((p = rb->strrchr(dir, '/')) != NULL);
753 return false;
756 /* return true if a lrc file is found */
757 static bool find_lrc_file(void)
759 reset_current_data();
761 DEBUGF("find lrc file for `%s'\n", current.mp3_file);
762 /* find .lrc file */
763 if (find_lrc_file_helper(""))
764 return true;
765 if (prefs.lrc_directory[0] == '/' && rb->dir_exists(prefs.lrc_directory))
767 if (find_lrc_file_helper(prefs.lrc_directory))
768 return true;
771 current.lrc_file[0] = 0;
772 return false;
775 /*******************************
776 * Load file.
777 *******************************/
779 /* check tag format and calculate value of the tag.
780 * supported tag: ti, ar, offset
781 * supported format of time tag: [mm:ss], [mm:ss.xx], [mm:ss.xxx]
782 * returns value of timega if tag is time tag, -1 if tag is supported tag,
783 * -10 otherwise.
785 static char *parse_int(char *ptr, int *val)
787 *val = rb->atoi(ptr);
788 while (isdigit(*ptr)) ptr++;
789 return ptr;
791 static long get_time_value(char *tag, bool read_id_tags, off_t file_offset)
793 long time;
794 char *ptr;
795 int val;
797 if (read_id_tags)
799 if (!rb->strncmp(tag, "ti:", 3))
801 if (!current.id3->title || rb->strcmp(&tag[3], current.id3->title))
802 current.title = lrcbufadd(&tag[3], false);
803 return -1;
805 if (!rb->strncmp(tag, "ar:", 3))
807 if (!current.id3->artist || rb->strcmp(&tag[3], current.id3->artist))
808 current.artist = lrcbufadd(&tag[3], false);
809 return -1;
811 if (!rb->strncmp(tag, "offset:", 7))
813 current.offset = rb->atoi(&tag[7]);
814 current.offset_file_offset = file_offset;
815 return -1;
819 /* minute */
820 ptr = parse_int(tag, &val);
821 if (ptr-tag < 1 || ptr-tag > 2 || *ptr != ':')
822 return -10;
823 time = val * 60000;
824 /* second */
825 tag = ptr+1;
826 ptr = parse_int(tag, &val);
827 if (ptr-tag != 2 || (*ptr != '.' && *ptr != ':' && *ptr != '\0'))
828 return -10;
829 time += val * 1000;
831 if (*ptr != '\0')
833 /* milliseccond */
834 tag = ptr+1;
835 ptr = parse_int(tag, &val);
836 if (ptr-tag < 2 || ptr-tag > 3 || *ptr != '\0')
837 return -10;
838 time += ((ptr-tag)==3 ?val: val*10);
841 return time;
844 /* format:
845 * [time tag]line
846 * [time tag]...[time tag]line
847 * [time tag]<word time tag>word<word time tag>...<word time tag>
849 static bool parse_lrc_line(char *line, off_t file_offset)
851 struct lrc_line *lrc_line = NULL, *first_lrc_line = NULL;
852 long time, time_start;
853 char *str, *tagstart, *tagend;
854 struct lrc_word *lrc_word;
855 int nword = 0;
857 /* parse [time tag]...[time tag] type tags */
858 str = line;
859 while (1)
861 if (*str != '[') break;
862 tagend = rb->strchr(str, ']');
863 if (tagend == NULL) break;
864 *tagend = 0;
865 time = get_time_value(str+1, !lrc_line, file_offset);
866 *tagend++ = ']';
867 if (time < 0)
868 break;
869 lrc_line = alloc_buf(sizeof(struct lrc_line));
870 if (lrc_line == NULL)
871 return false;
872 if (!first_lrc_line)
873 first_lrc_line = lrc_line;
874 lrc_line->file_offset = file_offset;
875 lrc_line->time_start = (time/10)*10;
876 lrc_line->old_time_start = lrc_line->time_start;
877 add_lrc_line(lrc_line, NULL);
878 file_offset += (long)tagend - (long)str;
879 str = tagend;
881 if (!first_lrc_line)
882 return true; /* no time tag in line */
884 lrc_line = first_lrc_line;
885 if (lrcbufadd("", false) == NULL)
886 return false;
888 /* parse <word time tag>...<word time tag> type tags */
889 /* [time tag]...[time tag]line type tags share lrc_line->words and can't
890 * use lrc_line->words->timestart. use lrc_line->time_start instead. */
891 time_start = -1;
892 tagstart = str;
893 while (*tagstart)
895 tagstart = rb->strchr(tagstart, '<');
896 if (!tagstart) break;
897 tagend = rb->strchr(tagstart, '>');
898 if (!tagend) break;
899 *tagend = 0;
900 time = get_time_value(tagstart+1, false,
901 file_offset + ((long)tagstart - (long)str));
902 *tagend++ = '>';
903 if (time < 0)
905 tagstart++;
906 continue;
908 *tagstart = 0;
909 /* found word time tag. */
910 if (*str || time_start != -1)
912 if ((lrc_word = new_lrc_word(time_start, str, true)) == NULL)
913 return false;
914 nword++;
916 file_offset += (long)tagend - (long)str;
917 tagstart = str = tagend;
918 time_start = time;
920 if ((lrc_word = new_lrc_word(time_start, str, true)) == NULL)
921 return false;
922 nword++;
924 /* duplicate lrc_lines */
925 while (lrc_line)
927 lrc_line->nword = nword;
928 lrc_line->words = lrc_word;
929 lrc_line = lrc_line->next;
932 return true;
935 /* format:
936 * \xa2\xe2hhmmssxx\xa2\xd0
937 * line 1
938 * line 2
939 * \xa2\xe2hhmmssxx\xa2\xd0
940 * line 3
941 * ...
943 static bool parse_snc_line(char *line, off_t file_offset)
945 #define SNC_TAG_START "\xa2\xe2"
946 #define SNC_TAG_END "\xa2\xd0"
948 /* SNC_TAG can be dencoded, so use
949 * temp_buf which contains native data */
950 if (!rb->memcmp(temp_buf, SNC_TAG_START, 2)
951 && !rb->memcmp(temp_buf+10, SNC_TAG_END, 2)) /* time tag */
953 const char *pos = temp_buf+2; /* skip SNC_TAG_START */
954 int hh, mm, ss, xx;
956 hh = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
957 mm = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
958 ss = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
959 xx = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
960 pos += 2; /* skip SNC_TAG_END */
962 /* initialize */
963 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
964 if (lrc_line == NULL)
965 return false;
966 lrc_line->file_offset = file_offset+2;
967 lrc_line->time_start = hh*3600000+mm*60000+ss*1000+xx*10;
968 lrc_line->old_time_start = lrc_line->time_start;
969 if (!add_lrc_line(lrc_line, ""))
970 return false;
971 if (pos[0]==0)
972 return true;
974 /* encode rest of line and add to buffer */
975 rb->iso_decode(pos, line, prefs.encoding, rb->strlen(pos)+1);
977 if (current.ll_head)
979 rb->strcat(line, "\n");
980 if (lrcbufadd(line, true) == NULL)
981 return false;
983 return true;
986 static bool parse_txt_line(char *line, off_t file_offset)
988 /* initialize */
989 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
990 if (lrc_line == NULL)
991 return false;
992 lrc_line->file_offset = file_offset;
993 lrc_line->time_start = 0;
994 lrc_line->old_time_start = -1;
995 if (!add_lrc_line(lrc_line, line))
996 return false;
997 return true;
1000 static void load_lrc_file(void)
1002 char utf8line[MAX_LINE_LEN*3];
1003 int fd;
1004 int encoding = prefs.encoding;
1005 bool (*line_parser)(char *line, off_t) = NULL;
1006 off_t file_offset, readsize;
1008 switch(current.type)
1010 case LRC8:
1011 encoding = UTF_8; /* .lrc8 is utf8 */
1012 /* fall through */
1013 case LRC:
1014 line_parser = parse_lrc_line;
1015 break;
1016 case SNC:
1017 line_parser = parse_snc_line;
1018 break;
1019 case TXT:
1020 line_parser = parse_txt_line;
1021 break;
1022 default:
1023 return;
1026 fd = rb->open(current.lrc_file, O_RDONLY);
1027 if (fd < 0) return;
1030 /* check encoding */
1031 #define BOM "\xef\xbb\xbf"
1032 #define BOM_SIZE 3
1033 unsigned char header[BOM_SIZE];
1034 unsigned char* (*utf_decode)(const unsigned char *,
1035 unsigned char *, int) = NULL;
1036 rb->read(fd, header, BOM_SIZE);
1037 if (!rb->memcmp(header, BOM, BOM_SIZE)) /* UTF-8 */
1039 encoding = UTF_8;
1041 else if (!rb->memcmp(header, "\xff\xfe", 2)) /* UTF-16LE */
1043 utf_decode = rb->utf16LEdecode;
1045 else if (!rb->memcmp(header, "\xfe\xff", 2)) /* UTF-16BE */
1047 utf_decode = rb->utf16BEdecode;
1049 else
1051 rb->lseek(fd, 0, SEEK_SET);
1054 if (utf_decode)
1056 /* convert encoding of file from UTF-16 to UTF-8 */
1057 char temp_file[MAX_PATH];
1058 int fe;
1059 rb->lseek(fd, 2, SEEK_SET);
1060 rb->snprintf(temp_file, MAX_PATH, "%s~", current.lrc_file);
1061 fe = rb->creat(temp_file, 0666);
1062 if (fe < 0)
1064 rb->close(fd);
1065 return;
1067 rb->write(fe, BOM, BOM_SIZE);
1068 while ((readsize = rb->read(fd, temp_buf, MAX_LINE_LEN)) > 0)
1070 char *end = utf_decode(temp_buf, utf8line, readsize/2);
1071 rb->write(fe, utf8line, end-utf8line);
1073 rb->close(fe);
1074 rb->close(fd);
1075 rb->remove(current.lrc_file);
1076 rb->rename(temp_file, current.lrc_file);
1077 fd = rb->open(current.lrc_file, O_RDONLY);
1078 if (fd < 0) return;
1079 rb->lseek(fd, BOM_SIZE, SEEK_SET); /* skip bom */
1080 encoding = UTF_8;
1084 file_offset = rb->lseek(fd, 0, SEEK_CUR); /* used in line_parser */
1085 while ((readsize = rb->read_line(fd, temp_buf, MAX_LINE_LEN)) > 0)
1087 /* note: parse_snc_line() reads temp_buf for native data. */
1088 rb->iso_decode(temp_buf, utf8line, encoding, readsize+1);
1089 if (!line_parser(utf8line, file_offset))
1090 break;
1091 file_offset += readsize;
1093 rb->close(fd);
1095 current.loaded_lrc = true;
1096 calc_brpos(NULL, 0);
1097 init_time_tag();
1099 return;
1102 #ifdef LRC_SUPPORT_ID3
1103 /*******************************
1104 * read lyrics from id3
1105 *******************************/
1106 /* taken from apps/metadata/mp3.c */
1107 static unsigned long unsync(unsigned long b0, unsigned long b1,
1108 unsigned long b2, unsigned long b3)
1110 return (((long)(b0 & 0x7F) << (3*7)) |
1111 ((long)(b1 & 0x7F) << (2*7)) |
1112 ((long)(b2 & 0x7F) << (1*7)) |
1113 ((long)(b3 & 0x7F) << (0*7)));
1116 static unsigned long bytes2int(unsigned long b0, unsigned long b1,
1117 unsigned long b2, unsigned long b3)
1119 return (((long)(b0 & 0xFF) << (3*8)) |
1120 ((long)(b1 & 0xFF) << (2*8)) |
1121 ((long)(b2 & 0xFF) << (1*8)) |
1122 ((long)(b3 & 0xFF) << (0*8)));
1125 static int unsynchronize(char* tag, int len, bool *ff_found)
1127 int i;
1128 unsigned char c;
1129 unsigned char *rp, *wp;
1130 bool _ff_found = false;
1131 if(ff_found) _ff_found = *ff_found;
1133 wp = rp = (unsigned char *)tag;
1135 rp = (unsigned char *)tag;
1136 for(i = 0; i<len; i++) {
1137 /* Read the next byte and write it back, but don't increment the
1138 write pointer */
1139 c = *rp++;
1140 *wp = c;
1141 if(_ff_found) {
1142 /* Increment the write pointer if it isn't an unsynch pattern */
1143 if(c != 0)
1144 wp++;
1145 _ff_found = false;
1146 } else {
1147 if(c == 0xff)
1148 _ff_found = true;
1149 wp++;
1152 if(ff_found) *ff_found = _ff_found;
1153 return (long)wp - (long)tag;
1156 static int read_unsynched(int fd, void *buf, int len, bool *ff_found)
1158 int i;
1159 int rc;
1160 int remaining = len;
1161 char *wp;
1163 wp = buf;
1165 while(remaining) {
1166 rc = rb->read(fd, wp, remaining);
1167 if(rc <= 0)
1168 return rc;
1170 i = unsynchronize(wp, remaining, ff_found);
1171 remaining -= i;
1172 wp += i;
1175 return len;
1178 static unsigned char* utf8cpy(const unsigned char *src,
1179 unsigned char *dst, int count)
1181 rb->strlcpy(dst, src, count+1);
1182 return dst+rb->strlen(dst);
1185 static void parse_id3v2(int fd)
1187 int minframesize;
1188 int size;
1189 long framelen;
1190 char header[10];
1191 char tmp[8];
1192 unsigned char version;
1193 int bytesread = 0;
1194 unsigned char global_flags;
1195 int flags;
1196 int skip;
1197 bool global_unsynch = false;
1198 bool global_ff_found = false;
1199 bool unsynch = false;
1200 int rc;
1201 enum {NOLT, SYLT, USLT} type = NOLT;
1203 /* Bail out if the tag is shorter than 10 bytes */
1204 if(current.id3->id3v2len < 10)
1205 return;
1207 /* Read the ID3 tag version from the header */
1208 if(10 != rb->read(fd, header, 10))
1209 return;
1211 /* Get the total ID3 tag size */
1212 size = current.id3->id3v2len - 10;
1214 version = current.id3->id3version;
1215 switch ( version )
1217 case ID3_VER_2_2:
1218 minframesize = 8;
1219 break;
1221 case ID3_VER_2_3:
1222 minframesize = 12;
1223 break;
1225 case ID3_VER_2_4:
1226 minframesize = 12;
1227 break;
1229 default:
1230 /* unsupported id3 version */
1231 return;
1234 global_flags = header[5];
1236 /* Skip the extended header if it is present */
1237 if(global_flags & 0x40) {
1238 skip = 0;
1240 if(version == ID3_VER_2_3) {
1241 if(10 != rb->read(fd, header, 10))
1242 return;
1243 /* The 2.3 extended header size doesn't include the header size
1244 field itself. Also, it is not unsynched. */
1245 framelen =
1246 bytes2int(header[0], header[1], header[2], header[3]) + 4;
1248 /* Skip the rest of the header */
1249 rb->lseek(fd, framelen - 10, SEEK_CUR);
1252 if(version >= ID3_VER_2_4) {
1253 if(4 != rb->read(fd, header, 4))
1254 return;
1256 /* The 2.4 extended header size does include the entire header,
1257 so here we can just skip it. This header is unsynched. */
1258 framelen = unsync(header[0], header[1],
1259 header[2], header[3]);
1261 rb->lseek(fd, framelen - 4, SEEK_CUR);
1265 /* Is unsynchronization applied? */
1266 if(global_flags & 0x80) {
1267 global_unsynch = true;
1270 /* We must have at least minframesize bytes left for the
1271 * remaining frames to be interesting */
1272 while (size >= minframesize) {
1273 flags = 0;
1275 /* Read frame header and check length */
1276 if(version >= ID3_VER_2_3) {
1277 if(global_unsynch && version <= ID3_VER_2_3)
1278 rc = read_unsynched(fd, header, 10, &global_ff_found);
1279 else
1280 rc = rb->read(fd, header, 10);
1281 if(rc != 10)
1282 return;
1283 /* Adjust for the 10 bytes we read */
1284 size -= 10;
1286 flags = bytes2int(0, 0, header[8], header[9]);
1288 if (version >= ID3_VER_2_4) {
1289 framelen = unsync(header[4], header[5],
1290 header[6], header[7]);
1291 } else {
1292 /* version .3 files don't use synchsafe ints for
1293 * size */
1294 framelen = bytes2int(header[4], header[5],
1295 header[6], header[7]);
1297 } else {
1298 if(6 != rb->read(fd, header, 6))
1299 return;
1300 /* Adjust for the 6 bytes we read */
1301 size -= 6;
1303 framelen = bytes2int(0, header[3], header[4], header[5]);
1306 if(framelen == 0){
1307 if (header[0] == 0 && header[1] == 0 && header[2] == 0)
1308 return;
1309 else
1310 continue;
1313 unsynch = false;
1315 if(flags)
1317 if (version >= ID3_VER_2_4) {
1318 if(flags & 0x0040) { /* Grouping identity */
1319 rb->lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
1320 framelen--;
1322 } else {
1323 if(flags & 0x0020) { /* Grouping identity */
1324 rb->lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
1325 framelen--;
1329 if(flags & 0x000c) /* Compression or encryption */
1331 /* Skip it */
1332 size -= framelen;
1333 rb->lseek(fd, framelen, SEEK_CUR);
1334 continue;
1337 if(flags & 0x0002) /* Unsynchronization */
1338 unsynch = true;
1340 if (version >= ID3_VER_2_4) {
1341 if(flags & 0x0001) { /* Data length indicator */
1342 if(4 != rb->read(fd, tmp, 4))
1343 return;
1345 /* We don't need the data length */
1346 framelen -= 4;
1351 if (framelen == 0)
1352 continue;
1354 if (framelen < 0)
1355 return;
1357 if(!rb->memcmp( header, "SLT", 3 ) ||
1358 !rb->memcmp( header, "SYLT", 4 ))
1360 /* found a supported tag */
1361 type = SYLT;
1362 break;
1364 else if(!rb->memcmp( header, "ULT", 3 ) ||
1365 !rb->memcmp( header, "USLT", 4 ))
1367 /* found a supported tag */
1368 type = USLT;
1369 break;
1371 else
1373 /* not a supported tag*/
1374 if(global_unsynch && version <= ID3_VER_2_3) {
1375 size -= read_unsynched(fd, lrc_buffer, framelen, &global_ff_found);
1376 } else {
1377 size -= framelen;
1378 if( rb->lseek(fd, framelen, SEEK_CUR) == -1 )
1379 return;
1383 if(type == NOLT)
1384 return;
1386 int encoding = 0, chsiz;
1387 char *tag, *p, utf8line[MAX_LINE_LEN*3];
1388 unsigned char* (*utf_decode)(const unsigned char *,
1389 unsigned char *, int) = NULL;
1390 /* use middle of lrc_buffer to store tag data. */
1391 if(framelen >= LRC_BUFFER_SIZE/3)
1392 framelen = LRC_BUFFER_SIZE/3-1;
1393 tag = lrc_buffer+LRC_BUFFER_SIZE*2/3-framelen-1;
1394 if(global_unsynch && version <= ID3_VER_2_3)
1395 bytesread = read_unsynched(fd, tag, framelen, &global_ff_found);
1396 else
1397 bytesread = rb->read(fd, tag, framelen);
1399 if( bytesread != framelen )
1400 return;
1402 if(unsynch || (global_unsynch && version >= ID3_VER_2_4))
1403 bytesread = unsynchronize(tag, bytesread, NULL);
1405 tag[bytesread] = 0;
1406 encoding = tag[0];
1407 p = tag;
1408 /* skip some data */
1409 if(type == SYLT) {
1410 p += 6;
1411 } else {
1412 p += 4;
1415 /* check encoding and skip content descriptor */
1416 switch (encoding) {
1417 case 0x01: /* Unicode with or without BOM */
1418 case 0x02:
1420 /* Now check if there is a BOM
1421 (zero-width non-breaking space, 0xfeff)
1422 and if it is in little or big endian format */
1423 if(!rb->memcmp(p, "\xff\xfe", 2)) { /* Little endian? */
1424 utf_decode = rb->utf16LEdecode;
1425 } else if(!rb->memcmp(p, "\xfe\xff", 2)) { /* Big endian? */
1426 utf_decode = rb->utf16BEdecode;
1427 } else
1428 utf_decode = NULL;
1430 encoding = NUM_CODEPAGES;
1431 do {
1432 size = p[0] | p[1];
1433 p += 2;
1434 } while(size);
1435 chsiz = 2;
1436 break;
1438 default:
1439 utf_decode = utf8cpy;
1440 if(encoding == 0x03) /* UTF-8 encoded string */
1441 encoding = UTF_8;
1442 else
1443 encoding = prefs.encoding;
1444 p += rb->strlen(p)+1;
1445 chsiz = 1;
1446 break;
1448 if(encoding == NUM_CODEPAGES)
1450 /* check if there is a BOM */
1451 if(!rb->memcmp(p, "\xff\xfe", 2)) { /* Little endian? */
1452 utf_decode = rb->utf16LEdecode;
1453 p += 2;
1454 } else if(!rb->memcmp(p, "\xfe\xff", 2)) { /* Big endian? */
1455 utf_decode = rb->utf16BEdecode;
1456 p += 2;
1457 } else if(!utf_decode) {
1458 /* If there is no BOM (which is a specification violation),
1459 let's try to guess it. If one of the bytes is 0x00, it is
1460 probably the most significant one. */
1461 if(p[1] == 0)
1462 utf_decode = rb->utf16LEdecode;
1463 else
1464 utf_decode = rb->utf16BEdecode;
1467 bytesread -= (long)p - (long)tag;
1468 tag = p;
1470 while ( bytesread > 0
1471 && lrc_buffer_used+bytesread < LRC_BUFFER_SIZE*2/3
1472 && LRC_BUFFER_SIZE*2/3 < lrc_buffer_end)
1474 bool is_crlf = false;
1475 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
1476 if(!lrc_line)
1477 break;
1478 lrc_line->file_offset = -1;
1479 if(type == USLT)
1481 /* replace 0x0a and 0x0d with 0x00 */
1482 p = tag;
1483 while(1) {
1484 utf_decode(p, tmp, 2);
1485 if(!tmp[0]) break;
1486 if(tmp[0] == 0x0d || tmp[0] == 0x0a)
1488 if(tmp[0] == 0x0d && tmp[1] == 0x0a)
1489 is_crlf = true;
1490 p[0] = 0;
1491 p[chsiz-1] = 0;
1492 break;
1494 p += chsiz;
1497 if(encoding == NUM_CODEPAGES)
1499 unsigned char* utf8 = utf8line;
1500 p = tag;
1501 do {
1502 utf8 = utf_decode(p, utf8, 1);
1503 p += 2;
1504 } while(*(utf8-1));
1506 else
1508 size = rb->strlen(tag)+1;
1509 rb->iso_decode(tag, utf8line, encoding, size);
1510 p = tag+size;
1513 if(type == SYLT) { /* timestamp */
1514 lrc_line->time_start = bytes2int(p[0], p[1], p[2], p[3]);
1515 lrc_line->old_time_start = lrc_line->time_start;
1516 p += 4;
1517 utf_decode(p, tmp, 1);
1518 if(tmp[0] == 0x0a)
1519 p += chsiz;
1520 } else { /* USLT */
1521 lrc_line->time_start = 0;
1522 lrc_line->old_time_start = -1;
1523 if(is_crlf) p += chsiz;
1525 bytesread -= (long)p - (long)tag;
1526 tag = p;
1527 if(!add_lrc_line(lrc_line, utf8line))
1528 break;
1531 current.type = ID3_SYLT-SYLT+type;
1532 rb->strcpy(current.lrc_file, current.mp3_file);
1534 current.loaded_lrc = true;
1535 calc_brpos(NULL, 0);
1536 init_time_tag();
1538 return;
1541 static bool read_id3(void)
1543 int fd;
1545 if(current.id3->codectype != AFMT_MPA_L1
1546 && current.id3->codectype != AFMT_MPA_L2
1547 && current.id3->codectype != AFMT_MPA_L3)
1548 return false;
1550 fd = rb->open(current.mp3_file, O_RDONLY);
1551 if(fd < 0) return false;
1552 current.loaded_lrc = false;
1553 parse_id3v2(fd);
1554 rb->close(fd);
1555 return current.loaded_lrc;
1557 #endif /* LRC_SUPPORT_ID3 */
1559 /*******************************
1560 * Display information
1561 *******************************/
1562 static void display_state(void)
1564 const char *str = NULL;
1566 if (AUDIO_STOP)
1567 str = "Audio Stopped";
1568 else if (current.found_lrc)
1570 if (!current.loaded_lrc)
1571 str = "Loading lrc";
1572 else if (!current.ll_head)
1573 str = "No lyrics";
1576 #ifdef HAVE_LCD_BITMAP
1577 const char *info = NULL;
1579 if (AUDIO_PLAY && prefs.display_title)
1581 char *title = (current.title? current.title: current.id3->title);
1582 char *artist = (current.artist? current.artist: current.id3->artist);
1584 if (artist != NULL && title != NULL)
1586 rb->snprintf(temp_buf, MAX_LINE_LEN, "%s/%s", title, artist);
1587 info = temp_buf;
1589 else if (title != NULL)
1590 info = title;
1591 else if (current.mp3_file[0] == '/')
1592 info = rb->strrchr(current.mp3_file, '/')+1;
1593 else
1594 info = "(no info)";
1597 int i, w, h;
1598 struct screen* display;
1599 FOR_NB_SCREENS(i)
1601 display = rb->screens[i];
1602 display->set_viewport(&vp_info[i]);
1603 display->clear_viewport();
1604 if (info)
1605 display->puts_scroll(0, 0, info);
1606 if (str)
1608 display->set_viewport(&vp_lyrics[i]);
1609 display->clear_viewport();
1610 display->getstringsize(str, &w, &h);
1611 if (vp_lyrics[i].width - w < 0)
1612 display->puts_scroll(0, vp_lyrics[i].height/font_ui_height/2,
1613 str);
1614 else
1615 display->putsxy((vp_lyrics[i].width - w)*prefs.align/2,
1616 (vp_lyrics[i].height-font_ui_height)/2, str);
1617 display->set_viewport(&vp_info[i]);
1619 display->update_viewport();
1620 display->set_viewport(NULL);
1622 #else
1623 /* there is no place to display title or artist. */
1624 rb->lcd_clear_display();
1625 if (str)
1626 rb->lcd_puts_scroll(0, 0, str);
1627 rb->lcd_update();
1628 #endif /* HAVE_LCD_BITMAP */
1631 static void display_time(void)
1633 rb->snprintf(temp_buf, MAX_LINE_LEN, "%ld:%02ld/%ld:%02ld",
1634 current.elapsed/60000, (current.elapsed/1000)%60,
1635 current.length/60000, (current.length)/1000%60);
1636 #ifdef HAVE_LCD_BITMAP
1637 int y = (prefs.display_title? font_ui_height:0), i;
1638 FOR_NB_SCREENS(i)
1640 struct screen* display = rb->screens[i];
1641 display->set_viewport(&vp_info[i]);
1642 display->setfont(FONT_SYSFIXED);
1643 display->putsxy(0, y, temp_buf);
1644 rb->gui_scrollbar_draw(display, 0, y+SYSFONT_HEIGHT+1,
1645 vp_info[i].width, SYSFONT_HEIGHT-2,
1646 current.length, 0, current.elapsed, HORIZONTAL);
1647 display->update_viewport_rect(0, y, vp_info[i].width, SYSFONT_HEIGHT*2);
1648 display->setfont(FONT_UI);
1649 display->set_viewport(NULL);
1651 #else
1652 rb->lcd_puts(0, 0, temp_buf);
1653 rb->lcd_update();
1654 #endif /* HAVE_LCD_BITMAP */
1657 /*******************************
1658 * Display lyrics
1659 *******************************/
1660 #ifdef HAVE_LCD_BITMAP
1661 static inline void set_to_default(struct screen *display)
1663 #if (LCD_DEPTH > 1)
1664 #ifdef HAVE_REMOTE_LCD
1665 if (display->screen_type != SCREEN_REMOTE)
1666 #endif
1667 display->set_foreground(prefs.active_color);
1668 #endif
1669 display->set_drawmode(DRMODE_SOLID);
1671 static inline void set_to_active(struct screen *display)
1673 #if (LCD_DEPTH > 1)
1674 #ifdef HAVE_REMOTE_LCD
1675 if (display->screen_type == SCREEN_REMOTE)
1676 display->set_drawmode(DRMODE_INVERSEVID);
1677 else
1678 #endif
1680 display->set_foreground(prefs.active_color);
1681 display->set_drawmode(DRMODE_SOLID);
1683 #else /* LCD_DEPTH == 1 */
1684 display->set_drawmode(DRMODE_INVERSEVID);
1685 #endif
1687 static inline void set_to_inactive(struct screen *display)
1689 #if (LCD_DEPTH > 1)
1690 #ifdef HAVE_REMOTE_LCD
1691 if (display->screen_type != SCREEN_REMOTE)
1692 #endif
1693 display->set_foreground(prefs.inactive_color);
1694 #endif
1695 display->set_drawmode(DRMODE_SOLID);
1698 static int display_lrc_line(struct lrc_line *lrc_line, int ypos, int i)
1700 struct screen *display = rb->screens[i];
1701 struct lrc_word *lrc_word;
1702 struct lrc_brpos *lrc_brpos;
1703 long time_start, time_end, elapsed;
1704 int count, width, nword;
1705 int xpos;
1706 const char *str;
1707 bool active_line;
1709 time_start = get_time_start(lrc_line);
1710 time_end = get_time_start(lrc_line->next);
1711 active_line = (time_start <= current.elapsed
1712 && time_end > current.elapsed);
1714 if (!lrc_line->width)
1716 /* empty line. draw bar during interval. */
1717 long rin = current.elapsed - time_start;
1718 long len = time_end - time_start;
1719 if (current.wipe && active_line && len >= 3500)
1721 elapsed = rin * vp_lyrics[i].width / len;
1722 set_to_inactive(display);
1723 display->fillrect(elapsed, ypos+font_ui_height/4+1,
1724 vp_lyrics[i].width-elapsed-1, font_ui_height/2-2);
1725 set_to_active(display);
1726 display->drawrect(0, ypos+font_ui_height/4,
1727 vp_lyrics[i].width, font_ui_height/2);
1728 display->fillrect(1, ypos+font_ui_height/4+1,
1729 elapsed-1, font_ui_height/2-2);
1730 set_to_default(display);
1732 return ypos + font_ui_height;
1735 lrc_brpos = calc_brpos(lrc_line, i);
1736 /* initialize line */
1737 xpos = (vp_lyrics[i].width - lrc_brpos->width)*prefs.align/2;
1738 count = 0;
1739 width = 0;
1741 active_line = active_line || !prefs.active_one_line;
1742 nword = lrc_line->nword-1;
1743 lrc_word = lrc_line->words + nword;
1744 str = lrc_word->word;
1745 /* only time_start of first word could be -1 */
1746 if (lrc_word->time_start != -1)
1747 time_end = get_word_time_start(lrc_word);
1748 else
1749 time_end = time_start;
1750 do {
1751 time_start = time_end;
1752 if (nword > 0)
1753 time_end = get_word_time_start(lrc_word-1);
1754 else
1755 time_end = get_time_start(lrc_line->next);
1757 if (time_start > current.elapsed || !active_line)
1759 /* inactive */
1760 elapsed = 0;
1762 else if (!current.wipe || time_end <= current.elapsed)
1764 /* active whole word */
1765 elapsed = lrc_word->width;
1767 else
1769 /* wipe word */
1770 long rin = current.elapsed - time_start;
1771 long len = time_end - time_start;
1772 elapsed = rin * lrc_word->width / len;
1775 int word_count = lrc_word->count;
1776 int word_width = lrc_word->width;
1777 set_to_active(display);
1778 while (word_count > 0 && word_width > 0)
1780 int c = lrc_brpos->count - count;
1781 int w = lrc_brpos->width - width;
1782 if (c > word_count || w > word_width)
1784 c = word_count;
1785 w = word_width;
1787 if (elapsed <= 0)
1789 set_to_inactive(display);
1791 else if (elapsed < w)
1793 /* wipe text */
1794 display->fillrect(xpos, ypos, elapsed, font_ui_height);
1795 set_to_inactive(display);
1796 display->fillrect(xpos+elapsed, ypos,
1797 w-elapsed, font_ui_height);
1798 #if (LCD_DEPTH > 1)
1799 #ifdef HAVE_REMOTE_LCD
1800 if (display->screen_type == SCREEN_REMOTE)
1801 display->set_drawmode(DRMODE_INVERSEVID);
1802 else
1803 #endif
1804 display->set_drawmode(DRMODE_BG);
1805 #else
1806 display->set_drawmode(DRMODE_INVERSEVID);
1807 #endif
1809 rb->strlcpy(temp_buf, str, c+1);
1810 display->putsxy(xpos, ypos, temp_buf);
1811 str += c;
1812 xpos += w;
1813 count += c;
1814 width += w;
1815 word_count -= c;
1816 word_width -= w;
1817 elapsed -= w;
1818 if (count >= lrc_brpos->count || width >= lrc_brpos->width)
1820 /* prepare for next line */
1821 lrc_brpos++;
1822 str = lrc_skip_space(str);
1823 xpos = (vp_lyrics[i].width - lrc_brpos->width)*prefs.align/2;
1824 ypos += font_ui_height;
1825 count = 0;
1826 width = 0;
1829 lrc_word--;
1830 } while (nword--);
1831 set_to_default(display);
1832 return ypos;
1834 #endif /* HAVE_LCD_BITMAP */
1836 static void display_lrcs(void)
1838 long time_start, time_end, rin, len;
1839 int i, nline[NB_SCREENS] = {0};
1840 struct lrc_line *lrc_center = current.ll_head;
1842 if (!lrc_center) return;
1844 while (get_time_start(lrc_center->next) <= current.elapsed)
1846 nline[SCREEN_MAIN] += lrc_center->nline[SCREEN_MAIN];
1847 #ifdef HAVE_REMOTE_LCD
1848 nline[SCREEN_REMOTE] += lrc_center->nline[SCREEN_REMOTE];
1849 #endif
1850 lrc_center = lrc_center->next;
1853 time_start = get_time_start(lrc_center);
1854 time_end = get_time_start(lrc_center->next);
1855 rin = current.elapsed - time_start;
1856 len = time_end - time_start;
1858 struct screen *display;
1859 FOR_NB_SCREENS(i)
1861 display = rb->screens[i];
1862 /* display current line at the center of the viewport */
1863 display->set_viewport(&vp_lyrics[i]);
1864 display->clear_viewport();
1865 #ifdef HAVE_LCD_BITMAP
1866 struct lrc_line *lrc_line;
1867 int y, ypos = 0, nblines = vp_lyrics[i].height/font_ui_height;
1868 y = (nblines-1)/2;
1869 if (rin < 0)
1871 /* current.elapsed < time of first lrc */
1872 if (!current.wipe)
1873 ypos = (time_start - current.elapsed)
1874 * font_ui_height / time_start;
1875 else
1876 y++;
1878 else if (len > 0)
1880 if (!current.wipe)
1881 ypos = - rin * lrc_center->nline[i] * font_ui_height / len;
1882 else
1884 long elapsed = rin * lrc_center->width / len;
1885 struct lrc_brpos *lrc_brpos = calc_brpos(lrc_center, i);
1886 while (elapsed > lrc_brpos->width)
1888 elapsed -= lrc_brpos->width;
1889 y--;
1890 lrc_brpos++;
1895 /* find first line to display */
1896 y -= nline[i];
1897 lrc_line = current.ll_head;
1898 while (y < -lrc_line->nline[i])
1900 y += lrc_line->nline[i];
1901 lrc_line = lrc_line->next;
1904 ypos += y*font_ui_height;
1905 while (lrc_line && ypos < vp_lyrics[i].height)
1907 ypos = display_lrc_line(lrc_line, ypos, i);
1908 lrc_line = lrc_line->next;
1910 if (!lrc_line && ypos < vp_lyrics[i].height)
1911 display->putsxy(0, ypos, "[end]");
1912 #else /* HAVE_LCD_CHARCELLS */
1913 struct lrc_line *lrc_line = lrc_center;
1914 struct lrc_brpos *lrc_brpos = calc_brpos(lrc_line, i);
1915 long elapsed = 0;
1916 const char *str = get_lrc_str(lrc_line);
1917 int x = vp_lyrics[i].width/2, y = 0;
1919 if (rin >= 0 && len > 0)
1921 elapsed = rin * lrc_center->width / len;
1922 while (elapsed > lrc_brpos->width)
1924 elapsed -= lrc_brpos->width;
1925 str = lrc_skip_space(str+lrc_brpos->count);
1926 lrc_brpos++;
1929 rb->strlcpy(temp_buf, str, lrc_brpos->count+1);
1931 x -= elapsed;
1932 if (x < 0)
1933 display->puts(0, y, temp_buf + rb->utf8seek(temp_buf, -x));
1934 else
1935 display->puts(x, y, temp_buf);
1936 x += rb->utf8length(temp_buf)+1;
1937 lrc_line = lrc_line->next;
1938 if (!lrc_line && x < vp_lyrics[i].width)
1940 if (x < vp_lyrics[i].width/2)
1941 x = vp_lyrics[i].width/2;
1942 display->puts(x, y, "[end]");
1944 #endif /* HAVE_LCD_BITMAP */
1945 display->update_viewport();
1946 display->set_viewport(NULL);
1950 /*******************************
1951 * Browse lyrics and edit time.
1952 *******************************/
1953 /* point playing line in lyrics */
1954 static enum themable_icons get_icon(int selected, void * data)
1956 (void) data;
1957 struct lrc_line *lrc_line = get_lrc_line(selected);
1958 if (lrc_line)
1960 long time_start = get_time_start(lrc_line);
1961 long time_end = get_time_start(lrc_line->next);
1962 long elapsed = current.id3->elapsed;
1963 if (time_start <= elapsed && time_end > elapsed)
1964 return Icon_Moving;
1966 return Icon_NOICON;
1968 static const char *get_lrc_timeline(int selected, void *data,
1969 char *buffer, size_t buffer_len)
1971 (void) data;
1972 struct lrc_line *lrc_line = get_lrc_line(selected);
1973 if (lrc_line)
1975 format_time_tag(temp_buf, get_time_start(lrc_line));
1976 rb->snprintf(buffer, buffer_len, "[%s]%s",
1977 temp_buf, get_lrc_str(lrc_line));
1978 return buffer;
1980 return NULL;
1983 static void save_changes(void)
1985 char new_file[MAX_PATH], *p;
1986 bool success = false;
1987 int fd, fe;
1988 if (!current.changed_lrc)
1989 return;
1990 rb->splash(HZ/2, "Saving changes...");
1991 if (current.type == TXT || current.type > NUM_TYPES)
1993 /* save changes to new .lrc file */
1994 rb->strcpy(new_file, current.lrc_file);
1995 p = rb->strrchr(new_file, '.');
1996 rb->strcpy(p, extentions[LRC]);
1998 else
2000 /* file already exists. use temp file. */
2001 rb->snprintf(new_file, MAX_PATH, "%s~", current.lrc_file);
2003 fd = rb->creat(new_file, 0666);
2004 fe = rb->open(current.lrc_file, O_RDONLY);
2005 if (fd >= 0 && fe >= 0)
2007 struct lrc_line *lrc_line, *temp_lrc;
2008 off_t curr = 0, next = 0, size = 0, offset = 0;
2009 for (lrc_line = current.ll_head; lrc_line;
2010 lrc_line = lrc_line->next)
2012 /* apply offset and set old_time_start -1 to indicate
2013 that time tag is not saved yet. */
2014 lrc_line->time_start = get_time_start(lrc_line);
2015 lrc_line->old_time_start = -1;
2017 current.offset = 0;
2018 if (current.type > NUM_TYPES)
2020 curr = -1;
2021 rb->write(fd, BOM, BOM_SIZE);
2023 else
2024 size = rb->filesize(fe);
2025 while (curr < size)
2027 /* find offset of next tag */
2028 lrc_line = NULL;
2029 for (temp_lrc = current.ll_head, next = size;
2030 temp_lrc; temp_lrc = temp_lrc->next)
2032 offset = temp_lrc->file_offset;
2033 if (offset < next && temp_lrc->old_time_start == -1)
2035 lrc_line = temp_lrc;
2036 next = offset;
2037 if (offset <= curr) break;
2040 offset = current.offset_file_offset;
2041 if (offset >= 0 && offset < next)
2043 lrc_line = NULL;
2044 next = offset;
2045 current.offset_file_offset = -1;
2047 if (next > curr)
2049 if (curr == -1) curr = 0;
2050 /* copy before the next tag */
2051 while (curr < next)
2053 ssize_t count = next-curr;
2054 if (count > MAX_LINE_LEN)
2055 count = MAX_LINE_LEN;
2056 if (rb->read(fe, temp_buf, count)!=count)
2057 break;
2058 rb->write(fd, temp_buf, count);
2059 curr += count;
2061 if (curr < next || curr >= size) break;
2063 /* write tag to new file and skip tag in backup */
2064 if (lrc_line != NULL)
2066 lrc_line->file_offset = rb->lseek(fd, 0, SEEK_CUR);
2067 lrc_line->old_time_start = lrc_line->time_start;
2068 long t = lrc_line->time_start;
2069 if (current.type == SNC)
2071 rb->fdprintf(fd, "%02ld%02ld%02ld%02ld", (t/3600000)%100,
2072 (t/60000)%60, (t/1000)%60, (t/10)%100);
2073 /* skip time tag */
2074 curr += rb->read(fe, temp_buf, 8);
2076 else /* LRC || LRC8 */
2078 format_time_tag(temp_buf, t);
2079 rb->fdprintf(fd, "[%s]", temp_buf);
2081 if (next == -1)
2083 rb->fdprintf(fd, "%s\n", get_lrc_str(lrc_line));
2086 if (current.type == LRC || current.type == LRC8)
2088 /* skip both time tag and offset tag */
2089 while (curr++<size && rb->read(fe, temp_buf, 1)==1)
2090 if (temp_buf[0]==']') break;
2093 success = (curr>=size);
2095 if (fe >= 0) rb->close(fe);
2096 if (fd >= 0) rb->close(fd);
2098 if (success)
2100 if (current.type == TXT || current.type > NUM_TYPES)
2102 current.type = LRC;
2103 rb->strcpy(current.lrc_file, new_file);
2105 else
2107 rb->remove(current.lrc_file);
2108 rb->rename(new_file, current.lrc_file);
2111 else
2113 rb->remove(new_file);
2114 rb->splash(HZ, "Could not save changes.");
2116 rb->reload_directory();
2117 current.changed_lrc = false;
2119 static int timetag_editor(void)
2121 struct gui_synclist gui_editor;
2122 struct lrc_line *lrc_line;
2123 bool exit = false;
2124 int button, idx, selected = 0;
2126 if (current.id3 == NULL || !current.ll_head)
2128 rb->splash(HZ, "No lyrics");
2129 return LRC_GOTO_MAIN;
2132 get_lrc_line(-1); /* initialize static variables */
2134 for (lrc_line = current.ll_head, idx = 0;
2135 lrc_line; lrc_line = lrc_line->next, idx++)
2137 long time_start = get_time_start(lrc_line);
2138 long time_end = get_time_start(lrc_line->next);
2139 long elapsed = current.id3->elapsed;
2140 if (time_start <= elapsed && time_end > elapsed)
2141 selected = idx;
2144 rb->gui_synclist_init(&gui_editor, &get_lrc_timeline, NULL, false, 1, NULL);
2145 rb->gui_synclist_set_nb_items(&gui_editor, current.nlrcline);
2146 rb->gui_synclist_set_icon_callback(&gui_editor, get_icon);
2147 rb->gui_synclist_set_title(&gui_editor, "Timetag Editor",
2148 Icon_Menu_functioncall);
2149 rb->gui_synclist_select_item(&gui_editor, selected);
2150 rb->gui_synclist_draw(&gui_editor);
2152 while (!exit)
2154 button = rb->get_action(CONTEXT_TREE, TIMEOUT_BLOCK);
2155 if (rb->gui_synclist_do_button(&gui_editor, &button,
2156 LIST_WRAP_UNLESS_HELD))
2157 continue;
2159 switch (button)
2161 case ACTION_STD_OK:
2162 idx = rb->gui_synclist_get_sel_pos(&gui_editor);
2163 lrc_line = get_lrc_line(idx);
2164 if (lrc_line)
2166 set_time_start(lrc_line, current.id3->elapsed-500);
2167 rb->gui_synclist_draw(&gui_editor);
2169 break;
2170 case ACTION_STD_CONTEXT:
2171 idx = rb->gui_synclist_get_sel_pos(&gui_editor);
2172 lrc_line = get_lrc_line(idx);
2173 if (lrc_line)
2175 long temp_time = get_time_start(lrc_line);
2176 if (lrc_set_time(get_lrc_str(lrc_line), NULL,
2177 &temp_time, 10, 0, current.length,
2178 LST_SET_MSEC|LST_SET_SEC|LST_SET_MIN) == 1)
2179 return PLUGIN_USB_CONNECTED;
2180 set_time_start(lrc_line, temp_time);
2181 rb->gui_synclist_draw(&gui_editor);
2183 break;
2184 case ACTION_TREE_STOP:
2185 case ACTION_STD_CANCEL:
2186 exit = true;
2187 break;
2188 default:
2189 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2190 return PLUGIN_USB_CONNECTED;
2191 break;
2195 FOR_NB_SCREENS(idx)
2196 rb->screens[idx]->stop_scroll();
2198 if (current.changed_lrc)
2200 MENUITEM_STRINGLIST(save_menu, "Save Changes?", NULL,
2201 "Yes", "No (save later)", "Discard All Changes")
2202 button = 0;
2203 exit = false;
2204 while (!exit)
2206 switch (rb->do_menu(&save_menu, &button, NULL, false))
2208 case 0:
2209 sort_lrcs();
2210 save_changes();
2211 exit = true;
2212 break;
2213 case 1:
2214 sort_lrcs();
2215 exit = true;
2216 break;
2217 case 2:
2218 init_time_tag();
2219 exit = true;
2220 break;
2221 case MENU_ATTACHED_USB:
2222 return PLUGIN_USB_CONNECTED;
2226 return LRC_GOTO_MAIN;
2229 /*******************************
2230 * Settings.
2231 *******************************/
2232 static void load_or_save_settings(bool save)
2234 static const char config_file[] = "lrcplayer.cfg";
2235 static struct configdata config[] = {
2236 #ifdef HAVE_LCD_COLOR
2237 { TYPE_INT, 0, 0xffffff, { .int_p = &prefs.inactive_color },
2238 "inactive color", NULL },
2239 #endif
2240 #ifdef HAVE_LCD_BITMAP
2241 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.wrap }, "wrap", NULL },
2242 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.wipe }, "wipe", NULL },
2243 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.active_one_line },
2244 "active one line", NULL },
2245 { TYPE_INT, 0, 2, { .int_p = &prefs.align }, "align", NULL },
2246 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.statusbar_on },
2247 "statusbar on", NULL },
2248 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.display_title },
2249 "display title", NULL },
2250 #endif
2251 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.display_time },
2252 "display time", NULL },
2253 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.backlight_on },
2254 "backlight on", NULL },
2256 { TYPE_STRING, 0, sizeof(prefs.lrc_directory),
2257 { .string = prefs.lrc_directory }, "lrc directory", NULL },
2258 { TYPE_INT, -1, NUM_CODEPAGES-1, { .int_p = &prefs.encoding },
2259 "encoding", NULL },
2260 #ifdef LRC_SUPPORT_ID3
2261 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.read_id3 }, "read id3", NULL },
2262 #endif
2265 if (!save)
2267 /* initialize setting */
2268 #if LCD_DEPTH > 1
2269 prefs.active_color = rb->lcd_get_foreground();
2270 prefs.inactive_color = LCD_LIGHTGRAY;
2271 #endif
2272 #ifdef HAVE_LCD_BITMAP
2273 prefs.wrap = true;
2274 prefs.wipe = true;
2275 prefs.active_one_line = false;
2276 prefs.align = 1; /* center */
2277 prefs.statusbar_on = false;
2278 prefs.display_title = true;
2279 #endif
2280 prefs.display_time = true;
2281 prefs.backlight_on = false;
2282 #ifdef LRC_SUPPORT_ID3
2283 prefs.read_id3 = true;
2284 #endif
2285 rb->strcpy(prefs.lrc_directory, "/Lyrics");
2286 prefs.encoding = -1; /* default codepage */
2288 configfile_load(config_file, config, ARRAYLEN(config), 0);
2290 else if (rb->memcmp(&old_prefs, &prefs, sizeof(prefs)))
2292 rb->splash(0, "Saving Settings");
2293 configfile_save(config_file, config, ARRAYLEN(config), 0);
2295 rb->memcpy(&old_prefs, &prefs, sizeof(prefs));
2298 static bool lrc_theme_menu(void)
2300 enum {
2301 #ifdef HAVE_LCD_BITMAP
2302 LRC_MENU_STATUSBAR,
2303 LRC_MENU_DISP_TITLE,
2304 #endif
2305 LRC_MENU_DISP_TIME,
2306 #ifdef HAVE_LCD_COLOR
2307 LRC_MENU_INACTIVE_COLOR,
2308 #endif
2309 LRC_MENU_BACKLIGHT,
2312 int selected = 0;
2313 bool exit = false, usb = false;
2315 MENUITEM_STRINGLIST(menu, "Theme Settings", NULL,
2316 #ifdef HAVE_LCD_BITMAP
2317 "Show Statusbar", "Display Title",
2318 #endif
2319 "Display Time",
2320 #ifdef HAVE_LCD_COLOR
2321 "Inactive Colour",
2322 #endif
2323 "Backlight Always On");
2325 while (!exit && !usb)
2327 switch (rb->do_menu(&menu, &selected, NULL, false))
2329 #ifdef HAVE_LCD_BITMAP
2330 case LRC_MENU_STATUSBAR:
2331 usb = rb->set_bool("Show Statusbar", &prefs.statusbar_on);
2332 break;
2333 case LRC_MENU_DISP_TITLE:
2334 usb = rb->set_bool("Display Title", &prefs.display_title);
2335 break;
2336 #endif
2337 case LRC_MENU_DISP_TIME:
2338 usb = rb->set_bool("Display Time", &prefs.display_time);
2339 break;
2340 #ifdef HAVE_LCD_COLOR
2341 case LRC_MENU_INACTIVE_COLOR:
2342 usb = rb->set_color(NULL, "Inactive Colour",
2343 &prefs.inactive_color, -1);
2344 break;
2345 #endif
2346 case LRC_MENU_BACKLIGHT:
2347 usb = rb->set_bool("Backlight Always On", &prefs.backlight_on);
2348 break;
2349 case MENU_ATTACHED_USB:
2350 usb = true;
2351 break;
2352 default:
2353 exit = true;
2354 break;
2358 return usb;
2361 #ifdef HAVE_LCD_BITMAP
2362 static bool lrc_display_menu(void)
2364 enum {
2365 LRC_MENU_WRAP,
2366 LRC_MENU_WIPE,
2367 LRC_MENU_ALIGN,
2368 LRC_MENU_LINE_MODE,
2371 int selected = 0;
2372 bool exit = false, usb = false;
2374 MENUITEM_STRINGLIST(menu, "Display Settings", NULL,
2375 "Wrap", "Wipe", "Alignment",
2376 "Activate Only Current Line");
2378 struct opt_items align_names[] = {
2379 {"Left", -1}, {"Centre", -1}, {"Right", -1},
2382 while (!exit && !usb)
2384 switch (rb->do_menu(&menu, &selected, NULL, false))
2386 case LRC_MENU_WRAP:
2387 usb = rb->set_bool("Wrap", &prefs.wrap);
2388 break;
2389 case LRC_MENU_WIPE:
2390 usb = rb->set_bool("Wipe", &prefs.wipe);
2391 break;
2392 case LRC_MENU_ALIGN:
2393 usb = rb->set_option("Alignment", &prefs.align, INT,
2394 align_names, 3, NULL);
2395 break;
2396 case LRC_MENU_LINE_MODE:
2397 usb = rb->set_bool("Activate Only Current Line",
2398 &prefs.active_one_line);
2399 break;
2400 case MENU_ATTACHED_USB:
2401 usb = true;
2402 break;
2403 default:
2404 exit = true;
2405 break;
2409 return usb;
2411 #endif /* HAVE_LCD_BITMAP */
2413 static bool lrc_lyrics_menu(void)
2415 enum {
2416 LRC_MENU_ENCODING,
2417 #ifdef LRC_SUPPORT_ID3
2418 LRC_MENU_READ_ID3,
2419 #endif
2420 LRC_MENU_LRC_DIR,
2423 int selected = 0;
2424 bool exit = false, usb = false;
2426 struct opt_items cp_names[NUM_CODEPAGES+1];
2427 int old_val;
2429 MENUITEM_STRINGLIST(menu, "Lyrics Settings", NULL,
2430 "Encoding",
2431 #ifdef LRC_SUPPORT_ID3
2432 "Read ID3 tag",
2433 #endif
2434 "Lrc Directry");
2436 cp_names[0].string = "Use default codepage";
2437 cp_names[0].voice_id = -1;
2438 for (old_val = 1; old_val < NUM_CODEPAGES+1; old_val++)
2440 cp_names[old_val].string = rb->get_codepage_name(old_val-1);
2441 cp_names[old_val].voice_id = -1;
2444 while (!exit && !usb)
2446 switch (rb->do_menu(&menu, &selected, NULL, false))
2448 case LRC_MENU_ENCODING:
2449 prefs.encoding++;
2450 old_val = prefs.encoding;
2451 usb = rb->set_option("Encoding", &prefs.encoding, INT,
2452 cp_names, NUM_CODEPAGES+1, NULL);
2453 if (prefs.encoding != old_val)
2455 save_changes();
2456 if (current.type < NUM_TYPES)
2458 /* let reload lrc file to apply encoding setting */
2459 reset_current_data();
2462 prefs.encoding--;
2463 break;
2464 #ifdef LRC_SUPPORT_ID3
2465 case LRC_MENU_READ_ID3:
2466 usb = rb->set_bool("Read ID3 tag", &prefs.read_id3);
2467 break;
2468 #endif
2469 case LRC_MENU_LRC_DIR:
2470 rb->strcpy(temp_buf, prefs.lrc_directory);
2471 if (!rb->kbd_input(temp_buf, sizeof(prefs.lrc_directory)))
2472 rb->strcpy(prefs.lrc_directory, temp_buf);
2473 break;
2474 case MENU_ATTACHED_USB:
2475 usb = true;
2476 break;
2477 default:
2478 exit = true;
2479 break;
2483 return usb;
2486 #ifdef LRC_DEBUG
2487 static const char* lrc_debug_data(int selected, void * data,
2488 char * buffer, size_t buffer_len)
2490 (void)data;
2491 switch (selected)
2493 case 0:
2494 rb->strlcpy(buffer, current.mp3_file, buffer_len);
2495 break;
2496 case 1:
2497 rb->strlcpy(buffer, current.lrc_file, buffer_len);
2498 break;
2499 case 2:
2500 rb->snprintf(buffer, buffer_len, "buf usage: %d,%d/%d",
2501 (int)lrc_buffer_used, (int)lrc_buffer_end,
2502 (int)lrc_buffer_size);
2503 break;
2504 case 3:
2505 rb->snprintf(buffer, buffer_len, "line count: %d,%d",
2506 current.nlrcline, current.nlrcbrpos);
2507 break;
2508 case 4:
2509 rb->snprintf(buffer, buffer_len, "loaded lrc? %s",
2510 current.loaded_lrc?"yes":"no");
2511 break;
2512 case 5:
2513 rb->snprintf(buffer, buffer_len, "too many lines? %s",
2514 current.too_many_lines?"yes":"no");
2515 break;
2516 default:
2517 return NULL;
2519 return buffer;
2522 static bool lrc_debug_menu(void)
2524 struct simplelist_info info;
2525 rb->simplelist_info_init(&info, "Debug Menu", 6, NULL);
2526 info.hide_selection = true;
2527 info.scroll_all = true;
2528 info.get_name = lrc_debug_data;
2529 return rb->simplelist_show_list(&info);
2531 #endif
2533 /* returns one of enum lrc_screen or enum plugin_status */
2534 static int lrc_menu(void)
2536 enum {
2537 LRC_MENU_THEME,
2538 #ifdef HAVE_LCD_BITMAP
2539 LRC_MENU_DISPLAY,
2540 #endif
2541 LRC_MENU_LYRICS,
2542 LRC_MENU_PLAYBACK,
2543 #ifdef LRC_DEBUG
2544 LRC_MENU_DEBUG,
2545 #endif
2546 LRC_MENU_OFFSET,
2547 LRC_MENU_TIMETAG_EDITOR,
2548 LRC_MENU_QUIT,
2551 MENUITEM_STRINGLIST(menu, "Lrcplayer Menu", NULL,
2552 "Theme Settings",
2553 #ifdef HAVE_LCD_BITMAP
2554 "Display Settings",
2555 #endif
2556 "Lyrics Settings",
2557 "Playback Control",
2558 #ifdef LRC_DEBUG
2559 "Debug Menu",
2560 #endif
2561 "Time Offset", "Timetag Editor",
2562 "Quit");
2563 int selected = 0, ret = LRC_GOTO_MENU;
2564 bool usb = false;
2566 while (ret == LRC_GOTO_MENU)
2568 switch (rb->do_menu(&menu, &selected, NULL, false))
2570 case LRC_MENU_THEME:
2571 usb = lrc_theme_menu();
2572 break;
2573 #ifdef HAVE_LCD_BITMAP
2574 case LRC_MENU_DISPLAY:
2575 usb = lrc_display_menu();
2576 break;
2577 #endif
2578 case LRC_MENU_LYRICS:
2579 usb = lrc_lyrics_menu();
2580 break;
2581 case LRC_MENU_PLAYBACK:
2582 usb = playback_control(NULL);
2583 ret = LRC_GOTO_MAIN;
2584 break;
2585 #ifdef LRC_DEBUG
2586 case LRC_MENU_DEBUG:
2587 usb = lrc_debug_menu();
2588 ret = LRC_GOTO_MAIN;
2589 break;
2590 #endif
2591 case LRC_MENU_OFFSET:
2592 usb = (lrc_set_time("Time Offset", "sec", &current.offset,
2593 10, -60*1000, 60*1000,
2594 LST_SET_MSEC|LST_SET_SEC) == 1);
2595 ret = LRC_GOTO_MAIN;
2596 break;
2597 case LRC_MENU_TIMETAG_EDITOR:
2598 ret = LRC_GOTO_EDITOR;
2599 break;
2600 case LRC_MENU_QUIT:
2601 ret = PLUGIN_OK;
2602 break;
2603 case MENU_ATTACHED_USB:
2604 usb = true;
2605 break;
2606 default:
2607 ret = LRC_GOTO_MAIN;
2608 break;
2610 if (usb)
2611 ret = PLUGIN_USB_CONNECTED;
2613 return ret;
2616 /*******************************
2617 * Main.
2618 *******************************/
2619 /* returns true if song has changed to know when to load new lyrics. */
2620 static bool check_audio_status(void)
2622 static int last_audio_status = 0;
2623 if (current.ff_rewind == -1)
2624 current.audio_status = rb->audio_status();
2625 current.id3 = rb->audio_current_track();
2626 if ((last_audio_status^current.audio_status)&AUDIO_STATUS_PLAY)
2628 last_audio_status = current.audio_status;
2629 return true;
2631 if (AUDIO_STOP || current.id3 == NULL)
2632 return false;
2633 if (rb->strcmp(current.mp3_file, current.id3->path))
2635 return true;
2637 return false;
2639 static void ff_rewind(long time, bool resume)
2641 if (AUDIO_PLAY)
2643 if (!AUDIO_PAUSE)
2645 resume = true;
2646 rb->audio_pause();
2648 rb->audio_ff_rewind(time);
2649 rb->sleep(HZ/10); /* take affect seeking */
2650 if (resume)
2651 rb->audio_resume();
2655 static int handle_button(void)
2657 int ret = LRC_GOTO_MAIN;
2658 static int step = 0;
2659 int limit, button = rb->get_action(CONTEXT_WPS, HZ/10);
2660 switch (button)
2662 case ACTION_WPS_BROWSE:
2663 #if CONFIG_KEYPAD == ONDIO_PAD
2664 /* ondio doesn't have ACTION_WPS_MENU,
2665 so use ACTION_WPS_BROWSE for menu */
2666 ret = LRC_GOTO_MENU;
2667 break;
2668 #endif
2669 case ACTION_WPS_STOP:
2670 save_changes();
2671 ret = PLUGIN_OK;
2672 break;
2673 case ACTION_WPS_PLAY:
2674 if (AUDIO_STOP && rb->global_status->resume_index != -1)
2676 if (rb->playlist_resume() != -1)
2678 rb->playlist_start(rb->global_status->resume_index,
2679 rb->global_status->resume_offset);
2682 else if (AUDIO_PAUSE)
2683 rb->audio_resume();
2684 else
2685 rb->audio_pause();
2686 break;
2687 case ACTION_WPS_SEEKFWD:
2688 case ACTION_WPS_SEEKBACK:
2689 if (AUDIO_STOP)
2690 break;
2691 if (current.ff_rewind > -1)
2693 if (button == ACTION_WPS_SEEKFWD)
2694 /* fast forwarding, calc max step relative to end */
2695 limit = (current.length - current.ff_rewind) * 3 / 100;
2696 else
2697 /* rewinding, calc max step relative to start */
2698 limit = (current.ff_rewind) * 3 / 100;
2699 limit = MAX(limit, 500);
2701 if (step > limit)
2702 step = limit;
2704 if (button == ACTION_WPS_SEEKFWD)
2705 current.ff_rewind += step;
2706 else
2707 current.ff_rewind -= step;
2709 if (current.ff_rewind > current.length-100)
2710 current.ff_rewind = current.length-100;
2711 if (current.ff_rewind < 0)
2712 current.ff_rewind = 0;
2714 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
2715 step += step >> (rb->global_settings->ff_rewind_accel + 3);
2717 else
2719 current.ff_rewind = current.elapsed;
2720 if (!AUDIO_PAUSE)
2721 rb->audio_pause();
2722 step = 1000 * rb->global_settings->ff_rewind_min_step;
2724 break;
2725 case ACTION_WPS_STOPSEEK:
2726 if (current.ff_rewind == -1)
2727 break;
2728 ff_rewind(current.ff_rewind, !AUDIO_PAUSE);
2729 current.elapsed = current.ff_rewind;
2730 current.ff_rewind = -1;
2731 break;
2732 case ACTION_WPS_SKIPNEXT:
2733 rb->audio_next();
2734 break;
2735 case ACTION_WPS_SKIPPREV:
2736 if (current.elapsed < 3000)
2737 rb->audio_prev();
2738 else
2739 ff_rewind(0, false);
2740 break;
2741 case ACTION_WPS_VOLDOWN:
2742 limit = rb->sound_min(SOUND_VOLUME);
2743 if (--rb->global_settings->volume < limit)
2744 rb->global_settings->volume = limit;
2745 rb->sound_set(SOUND_VOLUME, rb->global_settings->volume);
2746 break;
2747 case ACTION_WPS_VOLUP:
2748 limit = rb->sound_max(SOUND_VOLUME);
2749 if (++rb->global_settings->volume > limit)
2750 rb->global_settings->volume = limit;
2751 rb->sound_set(SOUND_VOLUME, rb->global_settings->volume);
2752 break;
2753 case ACTION_WPS_CONTEXT:
2754 ret = LRC_GOTO_EDITOR;
2755 break;
2756 case ACTION_WPS_MENU:
2757 ret = LRC_GOTO_MENU;
2758 break;
2759 default:
2760 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
2761 ret = PLUGIN_USB_CONNECTED;
2762 break;
2764 return ret;
2767 static int lrc_main(void)
2769 int ret = LRC_GOTO_MAIN;
2770 int i;
2771 long id3_timeout = 0;
2772 bool update_display_state = true;
2774 #ifdef HAVE_LCD_BITMAP
2775 /* y offset of vp_lyrics */
2776 int h = (prefs.display_title?font_ui_height:0)+
2777 (prefs.display_time?SYSFONT_HEIGHT*2:0);
2779 #endif
2781 FOR_NB_SCREENS(i)
2783 #ifdef HAVE_LCD_BITMAP
2784 rb->viewportmanager_theme_enable(i, prefs.statusbar_on, &vp_info[i]);
2785 vp_lyrics[i] = vp_info[i];
2786 vp_lyrics[i].flags &= ~VP_FLAG_ALIGNMENT_MASK;
2787 vp_lyrics[i].y += h;
2788 vp_lyrics[i].height -= h;
2789 #else
2790 rb->viewport_set_defaults(&vp_lyrics[i], i);
2791 if (prefs.display_time)
2793 vp_lyrics[i].y += 1; /* time */
2794 vp_lyrics[i].height -= 1;
2796 #endif
2799 if (prefs.backlight_on)
2800 backlight_ignore_timeout();
2802 #ifdef HAVE_LCD_BITMAP
2803 /* in case settings that may affect break position
2804 * are changed (statusbar_on and wrap). */
2805 if (!current.too_many_lines)
2806 calc_brpos(NULL, 0);
2807 #endif
2809 while (ret == LRC_GOTO_MAIN)
2811 if (check_audio_status())
2813 update_display_state = true;
2814 if (AUDIO_STOP)
2816 current.id3 = NULL;
2817 id3_timeout = 0;
2819 else if (rb->strcmp(current.mp3_file, current.id3->path))
2821 save_changes();
2822 reset_current_data();
2823 rb->strcpy(current.mp3_file, current.id3->path);
2824 id3_timeout = *rb->current_tick+HZ*3;
2825 current.found_lrc = false;
2828 if (current.id3 && current.id3->length)
2830 if (current.ff_rewind == -1)
2832 long di = current.id3->elapsed - current.elapsed;
2833 if (di < -250 || di > 0)
2834 current.elapsed = current.id3->elapsed;
2836 else
2837 current.elapsed = current.ff_rewind;
2838 current.length = current.id3->length;
2839 if (current.elapsed > current.length)
2840 current.elapsed = current.length;
2842 else
2844 current.elapsed = 0;
2845 current.length = 1;
2848 if (current.id3 && id3_timeout &&
2849 (TIME_AFTER(*rb->current_tick, id3_timeout) ||
2850 current.id3->artist))
2852 update_display_state = true;
2853 id3_timeout = 0;
2855 current.found_lrc = find_lrc_file();
2856 #ifdef LRC_SUPPORT_ID3
2857 if (!current.found_lrc && prefs.read_id3)
2859 /* no lyrics file found. try to read from id3 tag. */
2860 current.found_lrc = read_id3();
2862 #endif
2864 else if (current.found_lrc && !current.loaded_lrc)
2866 /* current.loaded_lrc is false after changing encode setting */
2867 update_display_state = true;
2868 display_state();
2869 load_lrc_file();
2871 if (update_display_state)
2873 #ifdef HAVE_LCD_BITMAP
2874 if (current.type == TXT || current.type == ID3_USLT)
2875 current.wipe = false;
2876 else
2877 current.wipe = prefs.wipe;
2878 #endif
2879 display_state();
2880 update_display_state = false;
2882 if (AUDIO_PLAY)
2884 if (prefs.display_time)
2885 display_time();
2886 if (!id3_timeout)
2887 display_lrcs();
2890 ret = handle_button();
2893 #ifdef HAVE_LCD_BITMAP
2894 FOR_NB_SCREENS(i)
2895 rb->viewportmanager_theme_undo(i, false);
2896 #endif
2897 if (prefs.backlight_on)
2898 backlight_use_settings();
2900 return ret;
2903 /* this is the plugin entry point */
2904 enum plugin_status plugin_start(const void* parameter)
2906 int ret = LRC_GOTO_MAIN;
2908 /* initialize settings. */
2909 load_or_save_settings(false);
2911 #ifdef HAVE_LCD_BITMAP
2912 rb->lcd_getstringsize("O", NULL, &font_ui_height);
2913 #endif
2915 lrc_buffer = rb->plugin_get_buffer(&lrc_buffer_size);
2916 lrc_buffer = (void *)(((long)lrc_buffer+3)&~3); /* 4 bytes aligned */
2917 lrc_buffer_size = (lrc_buffer_size - 4)&~3;
2919 reset_current_data();
2920 current.id3 = NULL;
2921 current.mp3_file[0] = 0;
2922 current.lrc_file[0] = 0;
2923 current.ff_rewind = -1;
2924 current.found_lrc = false;
2925 if (parameter && check_audio_status())
2927 const char *ext;
2928 rb->strcpy(current.mp3_file, current.id3->path);
2929 /* use passed parameter as lrc file. */
2930 rb->strcpy(current.lrc_file, parameter);
2931 if (!rb->file_exists(current.lrc_file))
2933 rb->splash(HZ, "Specified file dose not exist.");
2934 return PLUGIN_ERROR;
2936 ext = rb->strrchr(current.lrc_file, '.');
2937 if (!ext) ext = current.lrc_file;
2938 for (current.type = 0; current.type < NUM_TYPES; current.type++)
2940 if (!rb->strcasecmp(ext, extentions[current.type]))
2941 break;
2943 if (current.type == NUM_TYPES)
2945 rb->splashf(HZ, "%s is not supported", ext);
2946 return PLUGIN_ERROR;
2948 current.found_lrc = true;
2951 while (ret >= PLUGIN_OTHER)
2953 switch (ret)
2955 case LRC_GOTO_MAIN:
2956 ret = lrc_main();
2957 break;
2958 case LRC_GOTO_MENU:
2959 ret = lrc_menu();
2960 break;
2961 case LRC_GOTO_EDITOR:
2962 ret = timetag_editor();
2963 break;
2964 default:
2965 ret = PLUGIN_ERROR;
2966 break;
2970 load_or_save_settings(true);
2971 return ret;