1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "lib/playback_control.h"
23 #include "lib/configfile.h"
24 #include "lib/helper.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
35 /* define this to show debug info in menu */
36 /* #define LRC_DEBUG */
60 off_t file_offset
; /* offset of time tag in file */
63 short nline
[NB_SCREENS
];
64 struct lrc_line
*next
;
65 struct lrc_word
*words
;
69 /* display settings */
71 unsigned active_color
;
72 unsigned inactive_color
;
74 #ifdef HAVE_LCD_BITMAP
78 int align
; /* 0: left, 1: center, 2: right */
86 char lrc_directory
[64];
88 #ifdef LRC_SUPPORT_ID3
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
;
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 */
116 struct lrc_line
*ll_head
, **ll_tail
;
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 */
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
];
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 /*******************************
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
148 #else /* HAVE_LCD_BITMAP */
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
[] = {
156 #ifdef HAVE_REMOTE_LCD
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};
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;
170 if (!(flags
&LST_SET_TIME
))
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
;
183 rb
->button_clear_queue();
184 rb
->lcd_clear_display();
185 rb
->lcd_puts_scroll(0, LST_OFF_Y
, title
);
189 long abs_val
= value
;
190 long segvals
[4] = {-1, -1, -1, -1};
191 /* show negative value like -00:01 but 00:-1 */
198 /* calc value of each segments */
199 for (i
= pos_min
; i
<= pos_max
; i
++)
201 segvals
[i
] = abs_val
% maxs
[i
];
204 segvals
[i
-1] += abs_val
* maxs
[i
-1];
205 for (i
= pos_max
; i
>= pos_min
; i
--)
209 rb
->lcd_getstringsize(buffer
, &x
, &y
);
212 rb
->snprintf(&buffer
[len
], 32-len
, formats
[i
], segvals
[i
]);
213 len
+= rb
->strlen(&buffer
[len
]);
217 buffer
[len
-1] = 0; /* remove last separater */
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
)
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
);
232 rb
->lcd_put_cursor(x
+rb
->utf8length(&buffer
[p_start
])-1, y
, 0x7F);
236 int button
= pluginlib_getaction(TIMEOUT_BLOCK
, lst_contexts
, ARRAYLEN(lst_contexts
));
238 #ifdef HAVE_LCD_CHARCELLS
239 if (pos_min
!= pos_max
)
240 rb
->lcd_remove_cursor();
245 case PLA_DOWN_REPEAT
:
249 if (button
== PLA_DOWN_REPEAT
|| button
== PLA_DOWN
)
253 scl_step
= ((scls
[pos
]/scls
[pos_min
]+step
-1)/step
) * step
;
256 value
+= scl_step
* mult
;
263 case PLA_LEFT_REPEAT
:
268 case PLA_RIGHT_REPEAT
:
278 rb
->splash(HZ
, "Cancelled");
282 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
287 rb
->lcd_clear_display();
292 /*******************************
294 *******************************/
295 static void reset_current_data(void)
297 current
.title
= NULL
;
298 current
.artist
= NULL
;
300 current
.offset_file_offset
= -1;
301 current
.nlrcbrpos
= 0;
302 current
.nlrcline
= 0;
303 current
.ll_head
= NULL
;
304 current
.ll_tail
= ¤t
.ll_head
;
305 current
.loaded_lrc
= false;
306 current
.changed_lrc
= false;
307 current
.too_many_lines
= false;
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
)
321 rb
->strcpy(pos
, str
);
322 lrc_buffer_used
+= siz
;
325 static void *alloc_buf(size_t siz
)
328 if (lrc_buffer_used
+ siz
> lrc_buffer_end
)
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
)
338 if ((lrc_word
->word
= lrcbufadd(word
, join
)) == NULL
)
340 lrc_word
->time_start
= time_start
;
343 static bool add_lrc_line(struct lrc_line
*lrc_line
, char *word
)
346 lrc_line
->next
= NULL
;
347 lrc_line
->words
= NULL
;
350 if ((lrc_line
->words
= new_lrc_word(-1, word
, false)) == NULL
)
354 *current
.ll_tail
= lrc_line
;
355 current
.ll_tail
= &(lrc_line
->next
);
359 static struct lrc_line
*get_lrc_line(int idx
)
361 static struct lrc_line
*lrc_line
= NULL
;
365 lrc_line
= current
.ll_head
;
368 while (n
< idx
&& lrc_line
)
370 lrc_line
= lrc_line
->next
;
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
409 while (*str
&& *str
!= '\n' && isspace(*str
))
418 #ifdef HAVE_LCD_BITMAP
419 static bool isbrchr(const unsigned char *str
, int len
)
421 const unsigned char *p
= "!,-.:;? 、。!,.:;?―";
427 int n
= rb
->utf8seek(p
, 1);
428 if (len
== n
&& !rb
->strncmp(p
, str
, len
))
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
);
450 int word_count
, word_width
;
451 const unsigned char *str
;
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
);
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
)
468 lrc_brpos
= calc_brpos(lrc_line
, i
);
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
;
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
];
501 lrc_brpos
+= lrc_line
->nline
[--i
];
506 /* calculate number of lines, line width and char count for each line. */
508 cr
.nword
= lrc_line
->nword
;
509 lrc_word
= lrc_line
->words
+cr
.nword
;
510 cr
.str
= (lrc_word
-1)->word
;
523 while(cr
.nword
> 0 && cr
.str
>= (lrc_word
-1)->word
)
530 if (*cr
.str
== 0 || *cr
.str
== '\n')
534 #ifdef HAVE_LCD_CHARCELLS
535 c
= rb
->utf8seek(cr
.str
, 1);
538 c
= ((long)rb
->utf8decode(cr
.str
, &ch
) - (long)cr
.str
);
539 if (rb
->is_diacritic(ch
, NULL
))
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
)
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
;
572 lrc_word
->count
+= c
;
573 lrc_word
->width
+= w
;
576 lrc_line
->width
+= cr
.width
;
577 lrc_brpos
->count
= cr
.count
;
578 lrc_brpos
->width
= cr
.width
;
581 cr
.str
= lrc_skip_space(cr
.str
);
582 } while (*cr
.str
&& nlrcbrpos
< max_lrcbrpos
);
583 lrc_line
->nline
[i
] = nlrcbrpos
;
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
;
601 current
.ll_head
= NULL
;
602 current
.ll_tail
= ¤t
.ll_head
;
606 /* remove problematic lrc_lines.
607 * it would cause problem in display_lrc_line() if nword is 0. */
610 q
= p
->time_start
>= time_max
? current
.ll_tail
: ¤t
.ll_head
;
611 while ((*q
) && (*q
)->time_start
<= p
->time_start
)
617 time_max
= p
->time_start
;
618 current
.ll_tail
= &p
->next
;
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
;
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
];
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
;
654 current
.changed_lrc
= false;
657 /*******************************
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
670 /* taken from apps/recorder/albumart.c */
671 static void fix_filename(char* name
)
673 static const char invalid_chars
[] = "*/:<>?\\|";
681 else if (rb
->strchr(invalid_chars
, *name
))
686 static bool find_lrc_file_helper(const char *base_dir
)
688 char fname
[MAX_PATH
];
689 char *names
[3] = {NULL
, NULL
, NULL
};
692 /* /aaa/bbb/ccc/ddd.mp3
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
)
702 if (current
.id3
->title
&& rb
->strcmp(names
[0], current
.id3
->title
))
704 rb
->strlcpy(fname
, current
.id3
->title
, sizeof(fname
));
718 len
= rb
->snprintf(current
.lrc_file
, MAX_PATH
, "%s%s/",
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
);
735 DEBUGF("check file in %s\n", current
.lrc_file
);
736 if (!rb
->dir_exists(current
.lrc_file
))
738 for (current
.type
= 0; current
.type
< NUM_TYPES
; current
.type
++)
740 for (i
= 0; names
[i
] != NULL
; i
++)
742 rb
->snprintf(¤t
.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
);
752 } while ((p
= rb
->strrchr(dir
, '/')) != NULL
);
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
);
763 if (find_lrc_file_helper(""))
765 if (prefs
.lrc_directory
[0] == '/' && rb
->dir_exists(prefs
.lrc_directory
))
767 if (find_lrc_file_helper(prefs
.lrc_directory
))
771 current
.lrc_file
[0] = 0;
775 /*******************************
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,
785 static char *parse_int(char *ptr
, int *val
)
787 *val
= rb
->atoi(ptr
);
788 while (isdigit(*ptr
)) ptr
++;
791 static long get_time_value(char *tag
, bool read_id_tags
, off_t file_offset
)
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);
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);
811 if (!rb
->strncmp(tag
, "offset:", 7))
813 current
.offset
= rb
->atoi(&tag
[7]);
814 current
.offset_file_offset
= file_offset
;
820 ptr
= parse_int(tag
, &val
);
821 if (ptr
-tag
< 1 || ptr
-tag
> 2 || *ptr
!= ':')
826 ptr
= parse_int(tag
, &val
);
827 if (ptr
-tag
!= 2 || (*ptr
!= '.' && *ptr
!= ':' && *ptr
!= '\0'))
835 ptr
= parse_int(tag
, &val
);
836 if (ptr
-tag
< 2 || ptr
-tag
> 3 || *ptr
!= '\0')
838 time
+= ((ptr
-tag
)==3 ?val
: val
*10);
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
;
857 /* parse [time tag]...[time tag] type tags */
861 if (*str
!= '[') break;
862 tagend
= rb
->strchr(str
, ']');
863 if (tagend
== NULL
) break;
865 time
= get_time_value(str
+1, !lrc_line
, file_offset
);
869 lrc_line
= alloc_buf(sizeof(struct lrc_line
));
870 if (lrc_line
== NULL
)
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
;
882 return true; /* no time tag in line */
884 lrc_line
= first_lrc_line
;
885 if (lrcbufadd("", false) == NULL
)
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. */
895 tagstart
= rb
->strchr(tagstart
, '<');
896 if (!tagstart
) break;
897 tagend
= rb
->strchr(tagstart
, '>');
900 time
= get_time_value(tagstart
+1, false,
901 file_offset
+ ((long)tagstart
- (long)str
));
909 /* found word time tag. */
910 if (*str
|| time_start
!= -1)
912 if ((lrc_word
= new_lrc_word(time_start
, str
, true)) == NULL
)
916 file_offset
+= (long)tagend
- (long)str
;
917 tagstart
= str
= tagend
;
920 if ((lrc_word
= new_lrc_word(time_start
, str
, true)) == NULL
)
924 /* duplicate lrc_lines */
927 lrc_line
->nword
= nword
;
928 lrc_line
->words
= lrc_word
;
929 lrc_line
= lrc_line
->next
;
936 * \xa2\xe2hhmmssxx\xa2\xd0
939 * \xa2\xe2hhmmssxx\xa2\xd0
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 */
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 */
963 struct lrc_line
*lrc_line
= alloc_buf(sizeof(struct lrc_line
));
964 if (lrc_line
== NULL
)
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
, ""))
974 /* encode rest of line and add to buffer */
975 rb
->iso_decode(pos
, line
, prefs
.encoding
, rb
->strlen(pos
)+1);
979 rb
->strcat(line
, "\n");
980 if (lrcbufadd(line
, true) == NULL
)
986 static bool parse_txt_line(char *line
, off_t file_offset
)
989 struct lrc_line
*lrc_line
= alloc_buf(sizeof(struct lrc_line
));
990 if (lrc_line
== NULL
)
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
))
1000 static void load_lrc_file(void)
1002 char utf8line
[MAX_LINE_LEN
*3];
1004 int encoding
= prefs
.encoding
;
1005 bool (*line_parser
)(char *line
, off_t
) = NULL
;
1006 off_t file_offset
, readsize
;
1008 switch(current
.type
)
1011 encoding
= UTF_8
; /* .lrc8 is utf8 */
1014 line_parser
= parse_lrc_line
;
1017 line_parser
= parse_snc_line
;
1020 line_parser
= parse_txt_line
;
1026 fd
= rb
->open(current
.lrc_file
, O_RDONLY
);
1030 /* check encoding */
1031 #define BOM "\xef\xbb\xbf"
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 */
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
;
1051 rb
->lseek(fd
, 0, SEEK_SET
);
1056 /* convert encoding of file from UTF-16 to UTF-8 */
1057 char temp_file
[MAX_PATH
];
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);
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
);
1075 rb
->remove(current
.lrc_file
);
1076 rb
->rename(temp_file
, current
.lrc_file
);
1077 fd
= rb
->open(current
.lrc_file
, O_RDONLY
);
1079 rb
->lseek(fd
, BOM_SIZE
, SEEK_SET
); /* skip bom */
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
))
1091 file_offset
+= readsize
;
1095 current
.loaded_lrc
= true;
1096 calc_brpos(NULL
, 0);
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
)
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
1142 /* Increment the write pointer if it isn't an unsynch pattern */
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
)
1160 int remaining
= len
;
1166 rc
= rb
->read(fd
, wp
, remaining
);
1170 i
= unsynchronize(wp
, remaining
, ff_found
);
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
)
1192 unsigned char version
;
1194 unsigned char global_flags
;
1196 bool global_unsynch
= false;
1197 bool global_ff_found
= false;
1198 bool unsynch
= false;
1200 enum {NOLT
, SYLT
, USLT
} type
= NOLT
;
1202 /* Bail out if the tag is shorter than 10 bytes */
1203 if(current
.id3
->id3v2len
< 10)
1206 /* Read the ID3 tag version from the header */
1207 if(10 != rb
->read(fd
, header
, 10))
1210 /* Get the total ID3 tag size */
1211 size
= current
.id3
->id3v2len
- 10;
1213 version
= current
.id3
->id3version
;
1229 /* unsupported id3 version */
1233 global_flags
= header
[5];
1235 /* Skip the extended header if it is present */
1236 if(global_flags
& 0x40) {
1238 if(version
== ID3_VER_2_3
) {
1239 if(10 != rb
->read(fd
, header
, 10))
1241 /* The 2.3 extended header size doesn't include the header size
1242 field itself. Also, it is not unsynched. */
1244 bytes2int(header
[0], header
[1], header
[2], header
[3]) + 4;
1246 /* Skip the rest of the header */
1247 rb
->lseek(fd
, framelen
- 10, SEEK_CUR
);
1250 if(version
>= ID3_VER_2_4
) {
1251 if(4 != rb
->read(fd
, header
, 4))
1254 /* The 2.4 extended header size does include the entire header,
1255 so here we can just skip it. This header is unsynched. */
1256 framelen
= unsync(header
[0], header
[1],
1257 header
[2], header
[3]);
1259 rb
->lseek(fd
, framelen
- 4, SEEK_CUR
);
1263 /* Is unsynchronization applied? */
1264 if(global_flags
& 0x80) {
1265 global_unsynch
= true;
1268 /* We must have at least minframesize bytes left for the
1269 * remaining frames to be interesting */
1270 while (size
>= minframesize
) {
1273 /* Read frame header and check length */
1274 if(version
>= ID3_VER_2_3
) {
1275 if(global_unsynch
&& version
<= ID3_VER_2_3
)
1276 rc
= read_unsynched(fd
, header
, 10, &global_ff_found
);
1278 rc
= rb
->read(fd
, header
, 10);
1281 /* Adjust for the 10 bytes we read */
1284 flags
= bytes2int(0, 0, header
[8], header
[9]);
1286 if (version
>= ID3_VER_2_4
) {
1287 framelen
= unsync(header
[4], header
[5],
1288 header
[6], header
[7]);
1290 /* version .3 files don't use synchsafe ints for
1292 framelen
= bytes2int(header
[4], header
[5],
1293 header
[6], header
[7]);
1296 if(6 != rb
->read(fd
, header
, 6))
1298 /* Adjust for the 6 bytes we read */
1301 framelen
= bytes2int(0, header
[3], header
[4], header
[5]);
1305 if (header
[0] == 0 && header
[1] == 0 && header
[2] == 0)
1315 if (version
>= ID3_VER_2_4
) {
1316 if(flags
& 0x0040) { /* Grouping identity */
1317 rb
->lseek(fd
, 1, SEEK_CUR
); /* Skip 1 byte */
1321 if(flags
& 0x0020) { /* Grouping identity */
1322 rb
->lseek(fd
, 1, SEEK_CUR
); /* Skip 1 byte */
1327 if(flags
& 0x000c) /* Compression or encryption */
1331 rb
->lseek(fd
, framelen
, SEEK_CUR
);
1335 if(flags
& 0x0002) /* Unsynchronization */
1338 if (version
>= ID3_VER_2_4
) {
1339 if(flags
& 0x0001) { /* Data length indicator */
1340 if(4 != rb
->read(fd
, tmp
, 4))
1343 /* We don't need the data length */
1355 if(!rb
->memcmp( header
, "SLT", 3 ) ||
1356 !rb
->memcmp( header
, "SYLT", 4 ))
1358 /* found a supported tag */
1362 else if(!rb
->memcmp( header
, "ULT", 3 ) ||
1363 !rb
->memcmp( header
, "USLT", 4 ))
1365 /* found a supported tag */
1371 /* not a supported tag*/
1372 if(global_unsynch
&& version
<= ID3_VER_2_3
) {
1373 size
-= read_unsynched(fd
, lrc_buffer
, framelen
, &global_ff_found
);
1376 if( rb
->lseek(fd
, framelen
, SEEK_CUR
) == -1 )
1384 int encoding
= 0, chsiz
;
1385 char *tag
, *p
, utf8line
[MAX_LINE_LEN
*3];
1386 unsigned char* (*utf_decode
)(const unsigned char *,
1387 unsigned char *, int) = NULL
;
1388 /* use middle of lrc_buffer to store tag data. */
1389 if(framelen
>= LRC_BUFFER_SIZE
/3)
1390 framelen
= LRC_BUFFER_SIZE
/3-1;
1391 tag
= lrc_buffer
+LRC_BUFFER_SIZE
*2/3-framelen
-1;
1392 if(global_unsynch
&& version
<= ID3_VER_2_3
)
1393 bytesread
= read_unsynched(fd
, tag
, framelen
, &global_ff_found
);
1395 bytesread
= rb
->read(fd
, tag
, framelen
);
1397 if( bytesread
!= framelen
)
1400 if(unsynch
|| (global_unsynch
&& version
>= ID3_VER_2_4
))
1401 bytesread
= unsynchronize(tag
, bytesread
, NULL
);
1406 /* skip some data */
1413 /* check encoding and skip content descriptor */
1415 case 0x01: /* Unicode with or without BOM */
1418 /* Now check if there is a BOM
1419 (zero-width non-breaking space, 0xfeff)
1420 and if it is in little or big endian format */
1421 if(!rb
->memcmp(p
, "\xff\xfe", 2)) { /* Little endian? */
1422 utf_decode
= rb
->utf16LEdecode
;
1423 } else if(!rb
->memcmp(p
, "\xfe\xff", 2)) { /* Big endian? */
1424 utf_decode
= rb
->utf16BEdecode
;
1428 encoding
= NUM_CODEPAGES
;
1437 utf_decode
= utf8cpy
;
1438 if(encoding
== 0x03) /* UTF-8 encoded string */
1441 encoding
= prefs
.encoding
;
1442 p
+= rb
->strlen(p
)+1;
1446 if(encoding
== NUM_CODEPAGES
)
1448 /* check if there is a BOM */
1449 if(!rb
->memcmp(p
, "\xff\xfe", 2)) { /* Little endian? */
1450 utf_decode
= rb
->utf16LEdecode
;
1452 } else if(!rb
->memcmp(p
, "\xfe\xff", 2)) { /* Big endian? */
1453 utf_decode
= rb
->utf16BEdecode
;
1455 } else if(!utf_decode
) {
1456 /* If there is no BOM (which is a specification violation),
1457 let's try to guess it. If one of the bytes is 0x00, it is
1458 probably the most significant one. */
1460 utf_decode
= rb
->utf16LEdecode
;
1462 utf_decode
= rb
->utf16BEdecode
;
1465 bytesread
-= (long)p
- (long)tag
;
1468 while ( bytesread
> 0
1469 && lrc_buffer_used
+bytesread
< LRC_BUFFER_SIZE
*2/3
1470 && LRC_BUFFER_SIZE
*2/3 < lrc_buffer_end
)
1472 bool is_crlf
= false;
1473 struct lrc_line
*lrc_line
= alloc_buf(sizeof(struct lrc_line
));
1476 lrc_line
->file_offset
= -1;
1479 /* replace 0x0a and 0x0d with 0x00 */
1482 utf_decode(p
, tmp
, 2);
1484 if(tmp
[0] == 0x0d || tmp
[0] == 0x0a)
1486 if(tmp
[0] == 0x0d && tmp
[1] == 0x0a)
1495 if(encoding
== NUM_CODEPAGES
)
1497 unsigned char* utf8
= utf8line
;
1500 utf8
= utf_decode(p
, utf8
, 1);
1506 size
= rb
->strlen(tag
)+1;
1507 rb
->iso_decode(tag
, utf8line
, encoding
, size
);
1511 if(type
== SYLT
) { /* timestamp */
1512 lrc_line
->time_start
= bytes2int(p
[0], p
[1], p
[2], p
[3]);
1513 lrc_line
->old_time_start
= lrc_line
->time_start
;
1515 utf_decode(p
, tmp
, 1);
1519 lrc_line
->time_start
= 0;
1520 lrc_line
->old_time_start
= -1;
1521 if(is_crlf
) p
+= chsiz
;
1523 bytesread
-= (long)p
- (long)tag
;
1525 if(!add_lrc_line(lrc_line
, utf8line
))
1529 current
.type
= ID3_SYLT
-SYLT
+type
;
1530 rb
->strcpy(current
.lrc_file
, current
.mp3_file
);
1532 current
.loaded_lrc
= true;
1533 calc_brpos(NULL
, 0);
1539 static bool read_id3(void)
1543 if(current
.id3
->codectype
!= AFMT_MPA_L1
1544 && current
.id3
->codectype
!= AFMT_MPA_L2
1545 && current
.id3
->codectype
!= AFMT_MPA_L3
)
1548 fd
= rb
->open(current
.mp3_file
, O_RDONLY
);
1549 if(fd
< 0) return false;
1550 current
.loaded_lrc
= false;
1553 return current
.loaded_lrc
;
1555 #endif /* LRC_SUPPORT_ID3 */
1557 /*******************************
1558 * Display information
1559 *******************************/
1560 static void display_state(void)
1562 const char *str
= NULL
;
1565 str
= "Audio Stopped";
1566 else if (current
.found_lrc
)
1568 if (!current
.loaded_lrc
)
1569 str
= "Loading lrc";
1570 else if (!current
.ll_head
)
1574 #ifdef HAVE_LCD_BITMAP
1575 const char *info
= NULL
;
1577 if (AUDIO_PLAY
&& prefs
.display_title
)
1579 char *title
= (current
.title
? current
.title
: current
.id3
->title
);
1580 char *artist
= (current
.artist
? current
.artist
: current
.id3
->artist
);
1582 if (artist
!= NULL
&& title
!= NULL
)
1584 rb
->snprintf(temp_buf
, MAX_LINE_LEN
, "%s/%s", title
, artist
);
1587 else if (title
!= NULL
)
1589 else if (current
.mp3_file
[0] == '/')
1590 info
= rb
->strrchr(current
.mp3_file
, '/')+1;
1596 struct screen
* display
;
1599 display
= rb
->screens
[i
];
1600 display
->set_viewport(&vp_info
[i
]);
1601 display
->clear_viewport();
1603 display
->puts_scroll(0, 0, info
);
1606 display
->set_viewport(&vp_lyrics
[i
]);
1607 display
->clear_viewport();
1608 display
->getstringsize(str
, &w
, &h
);
1609 if (vp_lyrics
[i
].width
- w
< 0)
1610 display
->puts_scroll(0, vp_lyrics
[i
].height
/font_ui_height
/2,
1613 display
->putsxy((vp_lyrics
[i
].width
- w
)*prefs
.align
/2,
1614 (vp_lyrics
[i
].height
-font_ui_height
)/2, str
);
1615 display
->set_viewport(&vp_info
[i
]);
1617 display
->update_viewport();
1618 display
->set_viewport(NULL
);
1621 /* there is no place to display title or artist. */
1622 rb
->lcd_clear_display();
1624 rb
->lcd_puts_scroll(0, 0, str
);
1626 #endif /* HAVE_LCD_BITMAP */
1629 static void display_time(void)
1631 rb
->snprintf(temp_buf
, MAX_LINE_LEN
, "%ld:%02ld/%ld:%02ld",
1632 current
.elapsed
/60000, (current
.elapsed
/1000)%60,
1633 current
.length
/60000, (current
.length
)/1000%60);
1634 #ifdef HAVE_LCD_BITMAP
1635 int y
= (prefs
.display_title
? font_ui_height
:0), i
;
1638 struct screen
* display
= rb
->screens
[i
];
1639 display
->set_viewport(&vp_info
[i
]);
1640 display
->setfont(FONT_SYSFIXED
);
1641 display
->putsxy(0, y
, temp_buf
);
1642 rb
->gui_scrollbar_draw(display
, 0, y
+SYSFONT_HEIGHT
+1,
1643 vp_info
[i
].width
, SYSFONT_HEIGHT
-2,
1644 current
.length
, 0, current
.elapsed
, HORIZONTAL
);
1645 display
->update_viewport_rect(0, y
, vp_info
[i
].width
, SYSFONT_HEIGHT
*2);
1646 display
->setfont(FONT_UI
);
1647 display
->set_viewport(NULL
);
1650 rb
->lcd_puts(0, 0, temp_buf
);
1652 #endif /* HAVE_LCD_BITMAP */
1655 /*******************************
1657 *******************************/
1658 #ifdef HAVE_LCD_BITMAP
1659 static inline void set_to_default(struct screen
*display
)
1662 #ifdef HAVE_REMOTE_LCD
1663 if (display
->screen_type
!= SCREEN_REMOTE
)
1665 display
->set_foreground(prefs
.active_color
);
1667 display
->set_drawmode(DRMODE_SOLID
);
1669 static inline void set_to_active(struct screen
*display
)
1672 #ifdef HAVE_REMOTE_LCD
1673 if (display
->screen_type
== SCREEN_REMOTE
)
1674 display
->set_drawmode(DRMODE_INVERSEVID
);
1678 display
->set_foreground(prefs
.active_color
);
1679 display
->set_drawmode(DRMODE_SOLID
);
1681 #else /* LCD_DEPTH == 1 */
1682 display
->set_drawmode(DRMODE_INVERSEVID
);
1685 static inline void set_to_inactive(struct screen
*display
)
1688 #ifdef HAVE_REMOTE_LCD
1689 if (display
->screen_type
!= SCREEN_REMOTE
)
1691 display
->set_foreground(prefs
.inactive_color
);
1693 display
->set_drawmode(DRMODE_SOLID
);
1696 static int display_lrc_line(struct lrc_line
*lrc_line
, int ypos
, int i
)
1698 struct screen
*display
= rb
->screens
[i
];
1699 struct lrc_word
*lrc_word
;
1700 struct lrc_brpos
*lrc_brpos
;
1701 long time_start
, time_end
, elapsed
;
1702 int count
, width
, nword
;
1707 time_start
= get_time_start(lrc_line
);
1708 time_end
= get_time_start(lrc_line
->next
);
1709 active_line
= (time_start
<= current
.elapsed
1710 && time_end
> current
.elapsed
);
1712 if (!lrc_line
->width
)
1714 /* empty line. draw bar during interval. */
1715 long rin
= current
.elapsed
- time_start
;
1716 long len
= time_end
- time_start
;
1717 if (current
.wipe
&& active_line
&& len
>= 3500)
1719 elapsed
= rin
* vp_lyrics
[i
].width
/ len
;
1720 set_to_inactive(display
);
1721 display
->fillrect(elapsed
, ypos
+font_ui_height
/4+1,
1722 vp_lyrics
[i
].width
-elapsed
-1, font_ui_height
/2-2);
1723 set_to_active(display
);
1724 display
->drawrect(0, ypos
+font_ui_height
/4,
1725 vp_lyrics
[i
].width
, font_ui_height
/2);
1726 display
->fillrect(1, ypos
+font_ui_height
/4+1,
1727 elapsed
-1, font_ui_height
/2-2);
1728 set_to_default(display
);
1730 return ypos
+ font_ui_height
;
1733 lrc_brpos
= calc_brpos(lrc_line
, i
);
1734 /* initialize line */
1735 xpos
= (vp_lyrics
[i
].width
- lrc_brpos
->width
)*prefs
.align
/2;
1739 active_line
= active_line
|| !prefs
.active_one_line
;
1740 nword
= lrc_line
->nword
-1;
1741 lrc_word
= lrc_line
->words
+ nword
;
1742 str
= lrc_word
->word
;
1743 /* only time_start of first word could be -1 */
1744 if (lrc_word
->time_start
!= -1)
1745 time_end
= get_word_time_start(lrc_word
);
1747 time_end
= time_start
;
1749 time_start
= time_end
;
1751 time_end
= get_word_time_start(lrc_word
-1);
1753 time_end
= get_time_start(lrc_line
->next
);
1755 if (time_start
> current
.elapsed
|| !active_line
)
1760 else if (!current
.wipe
|| time_end
<= current
.elapsed
)
1762 /* active whole word */
1763 elapsed
= lrc_word
->width
;
1768 long rin
= current
.elapsed
- time_start
;
1769 long len
= time_end
- time_start
;
1770 elapsed
= rin
* lrc_word
->width
/ len
;
1773 int word_count
= lrc_word
->count
;
1774 int word_width
= lrc_word
->width
;
1775 set_to_active(display
);
1776 while (word_count
> 0 && word_width
> 0)
1778 int c
= lrc_brpos
->count
- count
;
1779 int w
= lrc_brpos
->width
- width
;
1780 if (c
> word_count
|| w
> word_width
)
1787 set_to_inactive(display
);
1789 else if (elapsed
< w
)
1792 display
->fillrect(xpos
, ypos
, elapsed
, font_ui_height
);
1793 set_to_inactive(display
);
1794 display
->fillrect(xpos
+elapsed
, ypos
,
1795 w
-elapsed
, font_ui_height
);
1797 #ifdef HAVE_REMOTE_LCD
1798 if (display
->screen_type
== SCREEN_REMOTE
)
1799 display
->set_drawmode(DRMODE_INVERSEVID
);
1802 display
->set_drawmode(DRMODE_BG
);
1804 display
->set_drawmode(DRMODE_INVERSEVID
);
1807 rb
->strlcpy(temp_buf
, str
, c
+1);
1808 display
->putsxy(xpos
, ypos
, temp_buf
);
1816 if (count
>= lrc_brpos
->count
|| width
>= lrc_brpos
->width
)
1818 /* prepare for next line */
1820 str
= lrc_skip_space(str
);
1821 xpos
= (vp_lyrics
[i
].width
- lrc_brpos
->width
)*prefs
.align
/2;
1822 ypos
+= font_ui_height
;
1829 set_to_default(display
);
1832 #endif /* HAVE_LCD_BITMAP */
1834 static void display_lrcs(void)
1836 long time_start
, time_end
, rin
, len
;
1837 int i
, nline
[NB_SCREENS
] = {0};
1838 struct lrc_line
*lrc_center
= current
.ll_head
;
1840 if (!lrc_center
) return;
1842 while (get_time_start(lrc_center
->next
) <= current
.elapsed
)
1844 nline
[SCREEN_MAIN
] += lrc_center
->nline
[SCREEN_MAIN
];
1845 #ifdef HAVE_REMOTE_LCD
1846 nline
[SCREEN_REMOTE
] += lrc_center
->nline
[SCREEN_REMOTE
];
1848 lrc_center
= lrc_center
->next
;
1851 time_start
= get_time_start(lrc_center
);
1852 time_end
= get_time_start(lrc_center
->next
);
1853 rin
= current
.elapsed
- time_start
;
1854 len
= time_end
- time_start
;
1856 struct screen
*display
;
1859 display
= rb
->screens
[i
];
1860 /* display current line at the center of the viewport */
1861 display
->set_viewport(&vp_lyrics
[i
]);
1862 display
->clear_viewport();
1863 #ifdef HAVE_LCD_BITMAP
1864 struct lrc_line
*lrc_line
;
1865 int y
, ypos
= 0, nblines
= vp_lyrics
[i
].height
/font_ui_height
;
1869 /* current.elapsed < time of first lrc */
1871 ypos
= (time_start
- current
.elapsed
)
1872 * font_ui_height
/ time_start
;
1879 ypos
= - rin
* lrc_center
->nline
[i
] * font_ui_height
/ len
;
1882 long elapsed
= rin
* lrc_center
->width
/ len
;
1883 struct lrc_brpos
*lrc_brpos
= calc_brpos(lrc_center
, i
);
1884 while (elapsed
> lrc_brpos
->width
)
1886 elapsed
-= lrc_brpos
->width
;
1893 /* find first line to display */
1895 lrc_line
= current
.ll_head
;
1896 while (y
< -lrc_line
->nline
[i
])
1898 y
+= lrc_line
->nline
[i
];
1899 lrc_line
= lrc_line
->next
;
1902 ypos
+= y
*font_ui_height
;
1903 while (lrc_line
&& ypos
< vp_lyrics
[i
].height
)
1905 ypos
= display_lrc_line(lrc_line
, ypos
, i
);
1906 lrc_line
= lrc_line
->next
;
1908 if (!lrc_line
&& ypos
< vp_lyrics
[i
].height
)
1909 display
->putsxy(0, ypos
, "[end]");
1910 #else /* HAVE_LCD_CHARCELLS */
1911 struct lrc_line
*lrc_line
= lrc_center
;
1912 struct lrc_brpos
*lrc_brpos
= calc_brpos(lrc_line
, i
);
1914 const char *str
= get_lrc_str(lrc_line
);
1915 int x
= vp_lyrics
[i
].width
/2, y
= 0;
1917 if (rin
>= 0 && len
> 0)
1919 elapsed
= rin
* lrc_center
->width
/ len
;
1920 while (elapsed
> lrc_brpos
->width
)
1922 elapsed
-= lrc_brpos
->width
;
1923 str
= lrc_skip_space(str
+lrc_brpos
->count
);
1927 rb
->strlcpy(temp_buf
, str
, lrc_brpos
->count
+1);
1931 display
->puts(0, y
, temp_buf
+ rb
->utf8seek(temp_buf
, -x
));
1933 display
->puts(x
, y
, temp_buf
);
1934 x
+= rb
->utf8length(temp_buf
)+1;
1935 lrc_line
= lrc_line
->next
;
1936 if (!lrc_line
&& x
< vp_lyrics
[i
].width
)
1938 if (x
< vp_lyrics
[i
].width
/2)
1939 x
= vp_lyrics
[i
].width
/2;
1940 display
->puts(x
, y
, "[end]");
1942 #endif /* HAVE_LCD_BITMAP */
1943 display
->update_viewport();
1944 display
->set_viewport(NULL
);
1948 /*******************************
1949 * Browse lyrics and edit time.
1950 *******************************/
1951 /* point playing line in lyrics */
1952 static enum themable_icons
get_icon(int selected
, void * data
)
1955 struct lrc_line
*lrc_line
= get_lrc_line(selected
);
1958 long time_start
= get_time_start(lrc_line
);
1959 long time_end
= get_time_start(lrc_line
->next
);
1960 long elapsed
= current
.id3
->elapsed
;
1961 if (time_start
<= elapsed
&& time_end
> elapsed
)
1966 static const char *get_lrc_timeline(int selected
, void *data
,
1967 char *buffer
, size_t buffer_len
)
1970 struct lrc_line
*lrc_line
= get_lrc_line(selected
);
1973 format_time_tag(temp_buf
, get_time_start(lrc_line
));
1974 rb
->snprintf(buffer
, buffer_len
, "[%s]%s",
1975 temp_buf
, get_lrc_str(lrc_line
));
1981 static void save_changes(void)
1983 char new_file
[MAX_PATH
], *p
;
1984 bool success
= false;
1986 if (!current
.changed_lrc
)
1988 rb
->splash(HZ
/2, "Saving changes...");
1989 if (current
.type
== TXT
|| current
.type
> NUM_TYPES
)
1991 /* save changes to new .lrc file */
1992 rb
->strcpy(new_file
, current
.lrc_file
);
1993 p
= rb
->strrchr(new_file
, '.');
1994 rb
->strcpy(p
, extentions
[LRC
]);
1998 /* file already exists. use temp file. */
1999 rb
->snprintf(new_file
, MAX_PATH
, "%s~", current
.lrc_file
);
2001 fd
= rb
->creat(new_file
, 0666);
2002 fe
= rb
->open(current
.lrc_file
, O_RDONLY
);
2003 if (fd
>= 0 && fe
>= 0)
2005 struct lrc_line
*lrc_line
, *temp_lrc
;
2006 off_t curr
= 0, next
= 0, size
= 0, offset
= 0;
2007 for (lrc_line
= current
.ll_head
; lrc_line
;
2008 lrc_line
= lrc_line
->next
)
2010 /* apply offset and set old_time_start -1 to indicate
2011 that time tag is not saved yet. */
2012 lrc_line
->time_start
= get_time_start(lrc_line
);
2013 lrc_line
->old_time_start
= -1;
2016 if (current
.type
> NUM_TYPES
)
2019 rb
->write(fd
, BOM
, BOM_SIZE
);
2022 size
= rb
->filesize(fe
);
2025 /* find offset of next tag */
2027 for (temp_lrc
= current
.ll_head
, next
= size
;
2028 temp_lrc
; temp_lrc
= temp_lrc
->next
)
2030 offset
= temp_lrc
->file_offset
;
2031 if (offset
< next
&& temp_lrc
->old_time_start
== -1)
2033 lrc_line
= temp_lrc
;
2035 if (offset
<= curr
) break;
2038 offset
= current
.offset_file_offset
;
2039 if (offset
>= 0 && offset
< next
)
2043 current
.offset_file_offset
= -1;
2047 if (curr
== -1) curr
= 0;
2048 /* copy before the next tag */
2051 ssize_t count
= next
-curr
;
2052 if (count
> MAX_LINE_LEN
)
2053 count
= MAX_LINE_LEN
;
2054 if (rb
->read(fe
, temp_buf
, count
)!=count
)
2056 rb
->write(fd
, temp_buf
, count
);
2059 if (curr
< next
|| curr
>= size
) break;
2061 /* write tag to new file and skip tag in backup */
2062 if (lrc_line
!= NULL
)
2064 lrc_line
->file_offset
= rb
->lseek(fd
, 0, SEEK_CUR
);
2065 lrc_line
->old_time_start
= lrc_line
->time_start
;
2066 long t
= lrc_line
->time_start
;
2067 if (current
.type
== SNC
)
2069 rb
->fdprintf(fd
, "%02ld%02ld%02ld%02ld", (t
/3600000)%100,
2070 (t
/60000)%60, (t
/1000)%60, (t
/10)%100);
2072 curr
+= rb
->read(fe
, temp_buf
, 8);
2074 else /* LRC || LRC8 */
2076 format_time_tag(temp_buf
, t
);
2077 rb
->fdprintf(fd
, "[%s]", temp_buf
);
2081 rb
->fdprintf(fd
, "%s\n", get_lrc_str(lrc_line
));
2084 if (current
.type
== LRC
|| current
.type
== LRC8
)
2086 /* skip both time tag and offset tag */
2087 while (curr
++<size
&& rb
->read(fe
, temp_buf
, 1)==1)
2088 if (temp_buf
[0]==']') break;
2091 success
= (curr
>=size
);
2093 if (fe
>= 0) rb
->close(fe
);
2094 if (fd
>= 0) rb
->close(fd
);
2098 if (current
.type
== TXT
|| current
.type
> NUM_TYPES
)
2101 rb
->strcpy(current
.lrc_file
, new_file
);
2105 rb
->remove(current
.lrc_file
);
2106 rb
->rename(new_file
, current
.lrc_file
);
2111 rb
->remove(new_file
);
2112 rb
->splash(HZ
, "Could not save changes.");
2114 rb
->reload_directory();
2115 current
.changed_lrc
= false;
2117 static int timetag_editor(void)
2119 struct gui_synclist gui_editor
;
2120 struct lrc_line
*lrc_line
;
2122 int button
, idx
, selected
= 0;
2124 if (current
.id3
== NULL
|| !current
.ll_head
)
2126 rb
->splash(HZ
, "No lyrics");
2127 return LRC_GOTO_MAIN
;
2130 get_lrc_line(-1); /* initialize static variables */
2132 for (lrc_line
= current
.ll_head
, idx
= 0;
2133 lrc_line
; lrc_line
= lrc_line
->next
, idx
++)
2135 long time_start
= get_time_start(lrc_line
);
2136 long time_end
= get_time_start(lrc_line
->next
);
2137 long elapsed
= current
.id3
->elapsed
;
2138 if (time_start
<= elapsed
&& time_end
> elapsed
)
2142 rb
->gui_synclist_init(&gui_editor
, &get_lrc_timeline
, NULL
, false, 1, NULL
);
2143 rb
->gui_synclist_set_nb_items(&gui_editor
, current
.nlrcline
);
2144 rb
->gui_synclist_set_icon_callback(&gui_editor
, get_icon
);
2145 rb
->gui_synclist_set_title(&gui_editor
, "Timetag Editor",
2146 Icon_Menu_functioncall
);
2147 rb
->gui_synclist_select_item(&gui_editor
, selected
);
2148 rb
->gui_synclist_draw(&gui_editor
);
2152 button
= rb
->get_action(CONTEXT_TREE
, TIMEOUT_BLOCK
);
2153 if (rb
->gui_synclist_do_button(&gui_editor
, &button
,
2154 LIST_WRAP_UNLESS_HELD
))
2160 idx
= rb
->gui_synclist_get_sel_pos(&gui_editor
);
2161 lrc_line
= get_lrc_line(idx
);
2164 set_time_start(lrc_line
, current
.id3
->elapsed
-500);
2165 rb
->gui_synclist_draw(&gui_editor
);
2168 case ACTION_STD_CONTEXT
:
2169 idx
= rb
->gui_synclist_get_sel_pos(&gui_editor
);
2170 lrc_line
= get_lrc_line(idx
);
2173 long temp_time
= get_time_start(lrc_line
);
2174 if (lrc_set_time(get_lrc_str(lrc_line
), NULL
,
2175 &temp_time
, 10, 0, current
.length
,
2176 LST_SET_MSEC
|LST_SET_SEC
|LST_SET_MIN
) == 1)
2177 return PLUGIN_USB_CONNECTED
;
2178 set_time_start(lrc_line
, temp_time
);
2179 rb
->gui_synclist_draw(&gui_editor
);
2182 case ACTION_TREE_STOP
:
2183 case ACTION_STD_CANCEL
:
2187 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
2188 return PLUGIN_USB_CONNECTED
;
2194 rb
->screens
[idx
]->stop_scroll();
2196 if (current
.changed_lrc
)
2198 MENUITEM_STRINGLIST(save_menu
, "Save Changes?", NULL
,
2199 "Yes", "No (save later)", "Discard All Changes")
2204 switch (rb
->do_menu(&save_menu
, &button
, NULL
, false))
2219 case MENU_ATTACHED_USB
:
2220 return PLUGIN_USB_CONNECTED
;
2224 return LRC_GOTO_MAIN
;
2227 /*******************************
2229 *******************************/
2230 static void load_or_save_settings(bool save
)
2232 static const char config_file
[] = "lrcplayer.cfg";
2233 static struct configdata config
[] = {
2234 #ifdef HAVE_LCD_COLOR
2235 { TYPE_INT
, 0, 0xffffff, { .int_p
= &prefs
.inactive_color
},
2236 "inactive color", NULL
},
2238 #ifdef HAVE_LCD_BITMAP
2239 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.wrap
}, "wrap", NULL
},
2240 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.wipe
}, "wipe", NULL
},
2241 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.active_one_line
},
2242 "active one line", NULL
},
2243 { TYPE_INT
, 0, 2, { .int_p
= &prefs
.align
}, "align", NULL
},
2244 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.statusbar_on
},
2245 "statusbar on", NULL
},
2246 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.display_title
},
2247 "display title", NULL
},
2249 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.display_time
},
2250 "display time", NULL
},
2251 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.backlight_on
},
2252 "backlight on", NULL
},
2254 { TYPE_STRING
, 0, sizeof(prefs
.lrc_directory
),
2255 { .string
= prefs
.lrc_directory
}, "lrc directory", NULL
},
2256 { TYPE_INT
, -1, NUM_CODEPAGES
-1, { .int_p
= &prefs
.encoding
},
2258 #ifdef LRC_SUPPORT_ID3
2259 { TYPE_BOOL
, 0, 1, { .bool_p
= &prefs
.read_id3
}, "read id3", NULL
},
2265 /* initialize setting */
2267 prefs
.active_color
= rb
->lcd_get_foreground();
2268 prefs
.inactive_color
= LCD_LIGHTGRAY
;
2270 #ifdef HAVE_LCD_BITMAP
2273 prefs
.active_one_line
= false;
2274 prefs
.align
= 1; /* center */
2275 prefs
.statusbar_on
= false;
2276 prefs
.display_title
= true;
2278 prefs
.display_time
= true;
2279 prefs
.backlight_on
= false;
2280 #ifdef LRC_SUPPORT_ID3
2281 prefs
.read_id3
= true;
2283 rb
->strcpy(prefs
.lrc_directory
, "/Lyrics");
2284 prefs
.encoding
= -1; /* default codepage */
2286 configfile_load(config_file
, config
, ARRAYLEN(config
), 0);
2288 else if (rb
->memcmp(&old_prefs
, &prefs
, sizeof(prefs
)))
2290 rb
->splash(0, "Saving Settings");
2291 configfile_save(config_file
, config
, ARRAYLEN(config
), 0);
2293 rb
->memcpy(&old_prefs
, &prefs
, sizeof(prefs
));
2296 static bool lrc_theme_menu(void)
2299 #ifdef HAVE_LCD_BITMAP
2301 LRC_MENU_DISP_TITLE
,
2304 #ifdef HAVE_LCD_COLOR
2305 LRC_MENU_INACTIVE_COLOR
,
2311 bool exit
= false, usb
= false;
2313 MENUITEM_STRINGLIST(menu
, "Theme Settings", NULL
,
2314 #ifdef HAVE_LCD_BITMAP
2315 "Show Statusbar", "Display Title",
2318 #ifdef HAVE_LCD_COLOR
2321 "Backlight Always On");
2323 while (!exit
&& !usb
)
2325 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
2327 #ifdef HAVE_LCD_BITMAP
2328 case LRC_MENU_STATUSBAR
:
2329 usb
= rb
->set_bool("Show Statusbar", &prefs
.statusbar_on
);
2331 case LRC_MENU_DISP_TITLE
:
2332 usb
= rb
->set_bool("Display Title", &prefs
.display_title
);
2335 case LRC_MENU_DISP_TIME
:
2336 usb
= rb
->set_bool("Display Time", &prefs
.display_time
);
2338 #ifdef HAVE_LCD_COLOR
2339 case LRC_MENU_INACTIVE_COLOR
:
2340 usb
= rb
->set_color(NULL
, "Inactive Colour",
2341 &prefs
.inactive_color
, -1);
2344 case LRC_MENU_BACKLIGHT
:
2345 usb
= rb
->set_bool("Backlight Always On", &prefs
.backlight_on
);
2347 case MENU_ATTACHED_USB
:
2359 #ifdef HAVE_LCD_BITMAP
2360 static bool lrc_display_menu(void)
2370 bool exit
= false, usb
= false;
2372 MENUITEM_STRINGLIST(menu
, "Display Settings", NULL
,
2373 "Wrap", "Wipe", "Alignment",
2374 "Activate Only Current Line");
2376 struct opt_items align_names
[] = {
2377 {"Left", -1}, {"Centre", -1}, {"Right", -1},
2380 while (!exit
&& !usb
)
2382 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
2385 usb
= rb
->set_bool("Wrap", &prefs
.wrap
);
2388 usb
= rb
->set_bool("Wipe", &prefs
.wipe
);
2390 case LRC_MENU_ALIGN
:
2391 usb
= rb
->set_option("Alignment", &prefs
.align
, INT
,
2392 align_names
, 3, NULL
);
2394 case LRC_MENU_LINE_MODE
:
2395 usb
= rb
->set_bool("Activate Only Current Line",
2396 &prefs
.active_one_line
);
2398 case MENU_ATTACHED_USB
:
2409 #endif /* HAVE_LCD_BITMAP */
2411 static bool lrc_lyrics_menu(void)
2415 #ifdef LRC_SUPPORT_ID3
2422 bool exit
= false, usb
= false;
2424 struct opt_items cp_names
[NUM_CODEPAGES
+1];
2427 MENUITEM_STRINGLIST(menu
, "Lyrics Settings", NULL
,
2429 #ifdef LRC_SUPPORT_ID3
2434 cp_names
[0].string
= "Use default codepage";
2435 cp_names
[0].voice_id
= -1;
2436 for (old_val
= 1; old_val
< NUM_CODEPAGES
+1; old_val
++)
2438 cp_names
[old_val
].string
= rb
->get_codepage_name(old_val
-1);
2439 cp_names
[old_val
].voice_id
= -1;
2442 while (!exit
&& !usb
)
2444 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
2446 case LRC_MENU_ENCODING
:
2448 old_val
= prefs
.encoding
;
2449 usb
= rb
->set_option("Encoding", &prefs
.encoding
, INT
,
2450 cp_names
, NUM_CODEPAGES
+1, NULL
);
2451 if (prefs
.encoding
!= old_val
)
2454 if (current
.type
< NUM_TYPES
)
2456 /* let reload lrc file to apply encoding setting */
2457 reset_current_data();
2462 #ifdef LRC_SUPPORT_ID3
2463 case LRC_MENU_READ_ID3
:
2464 usb
= rb
->set_bool("Read ID3 tag", &prefs
.read_id3
);
2467 case LRC_MENU_LRC_DIR
:
2468 rb
->strcpy(temp_buf
, prefs
.lrc_directory
);
2469 if (!rb
->kbd_input(temp_buf
, sizeof(prefs
.lrc_directory
)))
2470 rb
->strcpy(prefs
.lrc_directory
, temp_buf
);
2472 case MENU_ATTACHED_USB
:
2485 static const char* lrc_debug_data(int selected
, void * data
,
2486 char * buffer
, size_t buffer_len
)
2492 rb
->strlcpy(buffer
, current
.mp3_file
, buffer_len
);
2495 rb
->strlcpy(buffer
, current
.lrc_file
, buffer_len
);
2498 rb
->snprintf(buffer
, buffer_len
, "buf usage: %d,%d/%d",
2499 (int)lrc_buffer_used
, (int)lrc_buffer_end
,
2500 (int)lrc_buffer_size
);
2503 rb
->snprintf(buffer
, buffer_len
, "line count: %d,%d",
2504 current
.nlrcline
, current
.nlrcbrpos
);
2507 rb
->snprintf(buffer
, buffer_len
, "loaded lrc? %s",
2508 current
.loaded_lrc
?"yes":"no");
2511 rb
->snprintf(buffer
, buffer_len
, "too many lines? %s",
2512 current
.too_many_lines
?"yes":"no");
2520 static bool lrc_debug_menu(void)
2522 struct simplelist_info info
;
2523 rb
->simplelist_info_init(&info
, "Debug Menu", 6, NULL
);
2524 info
.hide_selection
= true;
2525 info
.scroll_all
= true;
2526 info
.get_name
= lrc_debug_data
;
2527 return rb
->simplelist_show_list(&info
);
2531 /* returns one of enum lrc_screen or enum plugin_status */
2532 static int lrc_menu(void)
2536 #ifdef HAVE_LCD_BITMAP
2545 LRC_MENU_TIMETAG_EDITOR
,
2549 MENUITEM_STRINGLIST(menu
, "Lrcplayer Menu", NULL
,
2551 #ifdef HAVE_LCD_BITMAP
2559 "Time Offset", "Timetag Editor",
2561 int selected
= 0, ret
= LRC_GOTO_MENU
;
2564 while (ret
== LRC_GOTO_MENU
)
2566 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
2568 case LRC_MENU_THEME
:
2569 usb
= lrc_theme_menu();
2571 #ifdef HAVE_LCD_BITMAP
2572 case LRC_MENU_DISPLAY
:
2573 usb
= lrc_display_menu();
2576 case LRC_MENU_LYRICS
:
2577 usb
= lrc_lyrics_menu();
2579 case LRC_MENU_PLAYBACK
:
2580 usb
= playback_control(NULL
);
2581 ret
= LRC_GOTO_MAIN
;
2584 case LRC_MENU_DEBUG
:
2585 usb
= lrc_debug_menu();
2586 ret
= LRC_GOTO_MAIN
;
2589 case LRC_MENU_OFFSET
:
2590 usb
= (lrc_set_time("Time Offset", "sec", ¤t
.offset
,
2591 10, -60*1000, 60*1000,
2592 LST_SET_MSEC
|LST_SET_SEC
) == 1);
2593 ret
= LRC_GOTO_MAIN
;
2595 case LRC_MENU_TIMETAG_EDITOR
:
2596 ret
= LRC_GOTO_EDITOR
;
2601 case MENU_ATTACHED_USB
:
2605 ret
= LRC_GOTO_MAIN
;
2609 ret
= PLUGIN_USB_CONNECTED
;
2614 /*******************************
2616 *******************************/
2617 /* returns true if song has changed to know when to load new lyrics. */
2618 static bool check_audio_status(void)
2620 static int last_audio_status
= 0;
2621 if (current
.ff_rewind
== -1)
2622 current
.audio_status
= rb
->audio_status();
2623 current
.id3
= rb
->audio_current_track();
2624 if ((last_audio_status
^current
.audio_status
)&AUDIO_STATUS_PLAY
)
2626 last_audio_status
= current
.audio_status
;
2629 if (AUDIO_STOP
|| current
.id3
== NULL
)
2631 if (rb
->strcmp(current
.mp3_file
, current
.id3
->path
))
2637 static void ff_rewind(long time
, bool resume
)
2646 rb
->audio_ff_rewind(time
);
2647 rb
->sleep(HZ
/10); /* take affect seeking */
2653 static int handle_button(void)
2655 int ret
= LRC_GOTO_MAIN
;
2656 static int step
= 0;
2657 int limit
, button
= rb
->get_action(CONTEXT_WPS
, HZ
/10);
2660 case ACTION_WPS_BROWSE
:
2661 #if CONFIG_KEYPAD == ONDIO_PAD
2662 /* ondio doesn't have ACTION_WPS_MENU,
2663 so use ACTION_WPS_BROWSE for menu */
2664 ret
= LRC_GOTO_MENU
;
2667 case ACTION_WPS_STOP
:
2671 case ACTION_WPS_PLAY
:
2672 if (AUDIO_STOP
&& rb
->global_status
->resume_index
!= -1)
2674 if (rb
->playlist_resume() != -1)
2676 rb
->playlist_start(rb
->global_status
->resume_index
,
2677 rb
->global_status
->resume_offset
);
2680 else if (AUDIO_PAUSE
)
2685 case ACTION_WPS_SEEKFWD
:
2686 case ACTION_WPS_SEEKBACK
:
2689 if (current
.ff_rewind
> -1)
2691 if (button
== ACTION_WPS_SEEKFWD
)
2692 /* fast forwarding, calc max step relative to end */
2693 limit
= (current
.length
- current
.ff_rewind
) * 3 / 100;
2695 /* rewinding, calc max step relative to start */
2696 limit
= (current
.ff_rewind
) * 3 / 100;
2697 limit
= MAX(limit
, 500);
2702 if (button
== ACTION_WPS_SEEKFWD
)
2703 current
.ff_rewind
+= step
;
2705 current
.ff_rewind
-= step
;
2707 if (current
.ff_rewind
> current
.length
-100)
2708 current
.ff_rewind
= current
.length
-100;
2709 if (current
.ff_rewind
< 0)
2710 current
.ff_rewind
= 0;
2712 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
2713 step
+= step
>> (rb
->global_settings
->ff_rewind_accel
+ 3);
2717 current
.ff_rewind
= current
.elapsed
;
2720 step
= 1000 * rb
->global_settings
->ff_rewind_min_step
;
2723 case ACTION_WPS_STOPSEEK
:
2724 if (current
.ff_rewind
== -1)
2726 ff_rewind(current
.ff_rewind
, !AUDIO_PAUSE
);
2727 current
.elapsed
= current
.ff_rewind
;
2728 current
.ff_rewind
= -1;
2730 case ACTION_WPS_SKIPNEXT
:
2733 case ACTION_WPS_SKIPPREV
:
2734 if (current
.elapsed
< 3000)
2737 ff_rewind(0, false);
2739 case ACTION_WPS_VOLDOWN
:
2740 limit
= rb
->sound_min(SOUND_VOLUME
);
2741 if (--rb
->global_settings
->volume
< limit
)
2742 rb
->global_settings
->volume
= limit
;
2743 rb
->sound_set(SOUND_VOLUME
, rb
->global_settings
->volume
);
2745 case ACTION_WPS_VOLUP
:
2746 limit
= rb
->sound_max(SOUND_VOLUME
);
2747 if (++rb
->global_settings
->volume
> limit
)
2748 rb
->global_settings
->volume
= limit
;
2749 rb
->sound_set(SOUND_VOLUME
, rb
->global_settings
->volume
);
2751 case ACTION_WPS_CONTEXT
:
2752 ret
= LRC_GOTO_EDITOR
;
2754 case ACTION_WPS_MENU
:
2755 ret
= LRC_GOTO_MENU
;
2758 if(rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
2759 ret
= PLUGIN_USB_CONNECTED
;
2765 static int lrc_main(void)
2767 int ret
= LRC_GOTO_MAIN
;
2769 long id3_timeout
= 0;
2770 bool update_display_state
= true;
2772 #ifdef HAVE_LCD_BITMAP
2773 /* y offset of vp_lyrics */
2774 int h
= (prefs
.display_title
?font_ui_height
:0)+
2775 (prefs
.display_time
?SYSFONT_HEIGHT
*2:0);
2781 #ifdef HAVE_LCD_BITMAP
2782 rb
->viewportmanager_theme_enable(i
, prefs
.statusbar_on
, &vp_info
[i
]);
2783 vp_lyrics
[i
] = vp_info
[i
];
2784 vp_lyrics
[i
].flags
&= ~VP_FLAG_ALIGNMENT_MASK
;
2785 vp_lyrics
[i
].y
+= h
;
2786 vp_lyrics
[i
].height
-= h
;
2788 rb
->viewport_set_defaults(&vp_lyrics
[i
], i
);
2789 if (prefs
.display_time
)
2791 vp_lyrics
[i
].y
+= 1; /* time */
2792 vp_lyrics
[i
].height
-= 1;
2797 if (prefs
.backlight_on
)
2798 backlight_ignore_timeout();
2800 #ifdef HAVE_LCD_BITMAP
2801 /* in case settings that may affect break position
2802 * are changed (statusbar_on and wrap). */
2803 if (!current
.too_many_lines
)
2804 calc_brpos(NULL
, 0);
2807 while (ret
== LRC_GOTO_MAIN
)
2809 if (check_audio_status())
2811 update_display_state
= true;
2817 else if (rb
->strcmp(current
.mp3_file
, current
.id3
->path
))
2820 reset_current_data();
2821 rb
->strcpy(current
.mp3_file
, current
.id3
->path
);
2822 id3_timeout
= *rb
->current_tick
+HZ
*3;
2823 current
.found_lrc
= false;
2826 if (current
.id3
&& current
.id3
->length
)
2828 if (current
.ff_rewind
== -1)
2830 long di
= current
.id3
->elapsed
- current
.elapsed
;
2831 if (di
< -250 || di
> 0)
2832 current
.elapsed
= current
.id3
->elapsed
;
2835 current
.elapsed
= current
.ff_rewind
;
2836 current
.length
= current
.id3
->length
;
2837 if (current
.elapsed
> current
.length
)
2838 current
.elapsed
= current
.length
;
2842 current
.elapsed
= 0;
2846 if (current
.id3
&& id3_timeout
&&
2847 (TIME_AFTER(*rb
->current_tick
, id3_timeout
) ||
2848 current
.id3
->artist
))
2850 update_display_state
= true;
2853 current
.found_lrc
= find_lrc_file();
2854 #ifdef LRC_SUPPORT_ID3
2855 if (!current
.found_lrc
&& prefs
.read_id3
)
2857 /* no lyrics file found. try to read from id3 tag. */
2858 current
.found_lrc
= read_id3();
2862 else if (current
.found_lrc
&& !current
.loaded_lrc
)
2864 /* current.loaded_lrc is false after changing encode setting */
2865 update_display_state
= true;
2869 if (update_display_state
)
2871 #ifdef HAVE_LCD_BITMAP
2872 if (current
.type
== TXT
|| current
.type
== ID3_USLT
)
2873 current
.wipe
= false;
2875 current
.wipe
= prefs
.wipe
;
2878 update_display_state
= false;
2882 if (prefs
.display_time
)
2888 ret
= handle_button();
2891 #ifdef HAVE_LCD_BITMAP
2893 rb
->viewportmanager_theme_undo(i
, false);
2895 if (prefs
.backlight_on
)
2896 backlight_use_settings();
2901 /* this is the plugin entry point */
2902 enum plugin_status
plugin_start(const void* parameter
)
2904 int ret
= LRC_GOTO_MAIN
;
2906 /* initialize settings. */
2907 load_or_save_settings(false);
2909 #ifdef HAVE_LCD_BITMAP
2910 rb
->lcd_getstringsize("O", NULL
, &font_ui_height
);
2913 lrc_buffer
= rb
->plugin_get_buffer(&lrc_buffer_size
);
2914 lrc_buffer
= (void *)(((long)lrc_buffer
+3)&~3); /* 4 bytes aligned */
2915 lrc_buffer_size
= (lrc_buffer_size
- 4)&~3;
2917 reset_current_data();
2919 current
.mp3_file
[0] = 0;
2920 current
.lrc_file
[0] = 0;
2921 current
.ff_rewind
= -1;
2922 current
.found_lrc
= false;
2923 if (parameter
&& check_audio_status())
2926 rb
->strcpy(current
.mp3_file
, current
.id3
->path
);
2927 /* use passed parameter as lrc file. */
2928 rb
->strcpy(current
.lrc_file
, parameter
);
2929 if (!rb
->file_exists(current
.lrc_file
))
2931 rb
->splash(HZ
, "Specified file dose not exist.");
2932 return PLUGIN_ERROR
;
2934 ext
= rb
->strrchr(current
.lrc_file
, '.');
2935 if (!ext
) ext
= current
.lrc_file
;
2936 for (current
.type
= 0; current
.type
< NUM_TYPES
; current
.type
++)
2938 if (!rb
->strcasecmp(ext
, extentions
[current
.type
]))
2941 if (current
.type
== NUM_TYPES
)
2943 rb
->splashf(HZ
, "%s is not supported", ext
);
2944 return PLUGIN_ERROR
;
2946 current
.found_lrc
= true;
2949 while (ret
>= PLUGIN_OTHER
)
2959 case LRC_GOTO_EDITOR
:
2960 ret
= timetag_editor();
2968 load_or_save_settings(true);