Fix wps image tags description.
[Rockbox.git] / apps / cuesheet.c
blob9c23d817bef4b6b673319c1d8249bf94a62389f6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin, Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <atoi.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include "system.h"
27 #include "audio.h"
28 #include "kernel.h"
29 #include "logf.h"
30 #include "sprintf.h"
31 #include "misc.h"
32 #include "screens.h"
33 #include "splash.h"
34 #include "list.h"
35 #include "action.h"
36 #include "lang.h"
37 #include "debug.h"
38 #include "settings.h"
39 #include "buffer.h"
40 #include "plugin.h"
41 #include "playback.h"
42 #include "cuesheet.h"
44 #define CUE_DIR ROCKBOX_DIR "/cue"
46 struct cuesheet *curr_cue;
47 struct cuesheet *temp_cue;
49 #if CONFIG_CODEC != SWCODEC
50 /* special trickery because the hwcodec playback engine is in firmware/ */
51 static bool cuesheet_handler(const char *filename)
53 return cuesheet_is_enabled() && look_for_cuesheet_file(filename, NULL);
55 #endif
57 void cuesheet_init(void)
59 if (global_settings.cuesheet) {
60 curr_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
61 temp_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
62 #if CONFIG_CODEC != SWCODEC
63 audio_set_cuesheet_callback(cuesheet_handler);
64 #endif
65 } else {
66 curr_cue = NULL;
67 temp_cue = NULL;
71 bool cuesheet_is_enabled(void)
73 return (curr_cue != NULL);
76 bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
78 DEBUGF("look for cue file\n");
80 char cuepath[MAX_PATH];
81 char *dot, *slash;
83 slash = strrchr(trackpath, '/');
84 if (!slash)
86 found_cue_path = NULL;
87 return false;
90 strncpy(cuepath, trackpath, MAX_PATH);
91 dot = strrchr(cuepath, '.');
92 strcpy(dot, ".cue");
94 int fd = open(cuepath,O_RDONLY);
95 if (fd < 0)
97 strcpy(cuepath, CUE_DIR);
98 strcat(cuepath, slash);
99 char *dot = strrchr(cuepath, '.');
100 strcpy(dot, ".cue");
101 fd = open(cuepath,O_RDONLY);
102 if (fd < 0)
104 if (found_cue_path)
105 found_cue_path = NULL;
106 return false;
109 close(fd);
110 if (found_cue_path)
111 strncpy(found_cue_path, cuepath, MAX_PATH);
112 return true;
115 static char *skip_whitespace(char* buf)
117 char *r = buf;
118 while (*r && isspace(*r))
119 r++;
120 return r;
124 static char *get_string(const char *line)
126 char *start, *end;
128 start = strchr(line, '"');
129 if (!start)
131 start = strchr(line, ' ');
133 if (!start)
134 return NULL;
137 end = strchr(++start, '"');
138 if (end)
139 *end = '\0';
141 return start;
144 /* parse cuesheet "file" and store the information in "cue" */
145 bool parse_cuesheet(char *file, struct cuesheet *cue)
147 char line[MAX_PATH];
148 char *s;
150 DEBUGF("cue parse\n");
151 int fd = open(file,O_RDONLY);
152 if (fd < 0)
154 /* couln't open the file */
155 return false;
158 /* Initialization */
159 memset(cue, 0, sizeof(struct cuesheet));
160 strcpy(cue->path, file);
161 cue->curr_track = cue->tracks;
163 while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
165 s = skip_whitespace(line);
167 if (!strncmp(s, "TRACK", 5))
169 cue->track_count++;
171 else if (!strncmp(s, "INDEX 01", 8))
173 s = strchr(s,' ');
174 s = skip_whitespace(s);
175 s = strchr(s,' ');
176 s = skip_whitespace(s);
177 cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
178 s = strchr(s,':') + 1;
179 cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
180 s = strchr(s,':') + 1;
181 cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
183 else if (!strncmp(s, "TITLE", 5)
184 || !strncmp(s, "PERFORMER", 9)
185 || !strncmp(s, "SONGWRITER", 10))
187 char *dest = NULL;
188 char *string = get_string(s);
189 if (!string)
190 break;
192 switch (*s)
194 case 'T': /* TITLE */
195 dest = (cue->track_count <= 0) ? cue->title :
196 cue->tracks[cue->track_count-1].title;
197 break;
199 case 'P': /* PERFORMER */
200 dest = (cue->track_count <= 0) ? cue->performer :
201 cue->tracks[cue->track_count-1].performer;
202 break;
204 case 'S': /* SONGWRITER */
205 dest = (cue->track_count <= 0) ? cue->songwriter :
206 cue->tracks[cue->track_count-1].songwriter;
207 break;
210 if (dest) {
211 dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
212 *dest = '\0';
216 close(fd);
218 /* If some songs don't have performer info, we copy the cuesheet performer */
219 int i;
220 for (i = 0; i < cue->track_count; i++)
222 if (*(cue->tracks[i].performer) == '\0')
223 strncpy(cue->tracks[i].performer, cue->performer, MAX_NAME);
225 if (*(cue->tracks[i].songwriter) == '\0')
226 strncpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME);
229 return true;
232 /* takes care of seeking to a track in a playlist
233 * returns false if audio isn't playing */
234 static bool seek(unsigned long pos)
236 if (!(audio_status() & AUDIO_STATUS_PLAY))
238 return false;
240 else
242 #if (CONFIG_CODEC == SWCODEC)
243 audio_pre_ff_rewind();
244 audio_ff_rewind(pos);
245 #else
246 audio_pause();
247 audio_ff_rewind(pos);
248 audio_resume();
249 #endif
250 return true;
254 /* returns the index of the track currently being played
255 and updates the information about the current track. */
256 int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
258 int i=0;
259 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
260 i++;
262 cue->curr_track_idx = i;
263 cue->curr_track = cue->tracks + i;
264 return i;
267 /* callback that gives list item titles for the cuesheet browser */
268 static char *list_get_name_cb(int selected_item,
269 void *data,
270 char *buffer)
272 struct cuesheet *cue = (struct cuesheet *)data;
274 if (selected_item & 1)
275 snprintf(buffer, MAX_PATH, "%s",
276 cue->tracks[selected_item/2].title);
277 else
278 snprintf(buffer, MAX_PATH, "%02d. %s", selected_item/2+1,
279 cue->tracks[selected_item/2].performer);
281 return buffer;
284 void browse_cuesheet(struct cuesheet *cue)
286 struct gui_synclist lists;
287 int action;
288 bool done = false;
289 int sel;
290 char title[MAX_PATH];
291 char cuepath[MAX_PATH];
292 struct mp3entry *id3 = audio_current_track();
294 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
295 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2, NULL);
296 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
297 gui_synclist_set_title(&lists, title, 0);
299 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
301 look_for_cuesheet_file(id3->path, cuepath);
304 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
306 gui_synclist_select_item(&lists,
307 2*cue_find_current_track(cue, id3->elapsed));
310 while (!done)
312 gui_synclist_draw(&lists);
313 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
314 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
315 continue;
316 switch (action)
318 case ACTION_STD_OK:
319 id3 = audio_current_track();
320 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
322 look_for_cuesheet_file(id3->path, cuepath);
323 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
325 sel = gui_synclist_get_sel_pos(&lists);
326 seek(cue->tracks[sel/2].offset);
329 break;
330 case ACTION_STD_CANCEL:
331 done = true;
336 bool display_cuesheet_content(char* filename)
338 size_t bufsize = 0;
339 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
340 if (!cue || bufsize < sizeof(struct cuesheet))
341 return false;
343 if (!parse_cuesheet(filename, cue))
344 return false;
346 browse_cuesheet(cue);
347 return true;
350 /* skips backwards or forward in the current cuesheet
351 * the return value indicates whether we're still in a cusheet after skipping
352 * it also returns false if we weren't in a cuesheet.
353 * direction should be 1 or -1.
355 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
357 int track = cue_find_current_track(curr_cue, curr_pos);
359 if (direction >= 0 && track == curr_cue->track_count - 1)
361 /* we want to get out of the cuesheet */
362 return false;
364 else
366 if (!(direction <= 0 && track == 0))
367 track += direction;
369 seek(curr_cue->tracks[track].offset);
370 return true;
375 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
377 if (!cue || !cue->curr_track)
378 return;
380 struct cue_track_info *track = cue->curr_track;
382 id3->title = *track->title ? track->title : NULL;
383 id3->artist = *track->performer ? track->performer : NULL;
384 id3->composer = *track->songwriter ? track->songwriter : NULL;
385 id3->album = *cue->title ? cue->title : NULL;
387 /* if the album artist is the same as the track artist, we hide it. */
388 if (strcmp(cue->performer, track->performer))
389 id3->albumartist = *cue->performer ? cue->performer : NULL;
390 else
391 id3->albumartist = NULL;
393 int i = cue->curr_track_idx;
394 id3->tracknum = i+1;
395 if (id3->track_string)
396 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
399 #ifdef HAVE_LCD_BITMAP
400 static inline void draw_veritcal_line_mark(struct screen * screen,
401 int x, int y, int h)
403 screen->set_drawmode(DRMODE_COMPLEMENT);
404 screen->vline(x, y, y+h-1);
407 /* draw the cuesheet markers for a track of length "tracklen",
408 between (x1,y) and (x2,y) */
409 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
410 int x1, int x2, int y, int h)
412 int i,xi;
413 int w = x2 - x1;
414 for (i=1; i < curr_cue->track_count; i++)
416 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
417 draw_veritcal_line_mark(screen, xi, y, h);
420 #endif