Oops.
[Rockbox.git] / apps / cuesheet.c
blob79a07a73c68e79dc6bff0e52871fc4cecd5fab77
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 #if CONFIG_CODEC != SWCODEC
47 /* special trickery because the hwcodec playback engine is in firmware/ */
48 static bool cuesheet_handler(const char *filename)
50 return cuesheet_is_enabled() && look_for_cuesheet_file(filename, NULL);
52 #endif
54 void cuesheet_init(void)
56 if (global_settings.cuesheet) {
57 curr_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
58 temp_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
59 #if CONFIG_CODEC != SWCODEC
60 audio_set_cuesheet_callback(cuesheet_handler);
61 #endif
62 } else {
63 curr_cue = NULL;
64 temp_cue = NULL;
68 bool cuesheet_is_enabled(void)
70 return (curr_cue != NULL);
73 bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
75 DEBUGF("look for cue file\n");
76 char cuepath[MAX_PATH];
77 strncpy(cuepath, trackpath, MAX_PATH);
78 char *dot = strrchr(cuepath, '.');
79 strcpy(dot, ".cue");
81 int fd = open(cuepath,O_RDONLY);
82 if (fd < 0)
84 strcpy(cuepath, CUE_DIR);
85 strcat(cuepath, strrchr(trackpath, '/'));
86 char *dot = strrchr(cuepath, '.');
87 strcpy(dot, ".cue");
88 fd = open(cuepath,O_RDONLY);
89 if (fd < 0)
91 if (found_cue_path)
92 found_cue_path = NULL;
93 return false;
96 close(fd);
97 if (found_cue_path)
98 strncpy(found_cue_path, cuepath, MAX_PATH);
99 return true;
102 static char *skip_whitespace(char* buf)
104 char *r = buf;
105 while (*r && isspace(*r))
106 r++;
107 return r;
111 static char *get_string(const char *line)
113 char *start, *end;
115 start = strchr(line, '"');
116 if (!start)
118 start = strchr(line, ' ');
120 if (!start)
121 return NULL;
124 end = strchr(++start, '"');
125 if (end)
126 *end = '\0';
128 return start;
131 /* parse cuesheet "file" and store the information in "cue" */
132 bool parse_cuesheet(char *file, struct cuesheet *cue)
134 char line[MAX_PATH];
135 char *s;
137 DEBUGF("cue parse\n");
138 int fd = open(file,O_RDONLY);
139 if (fd < 0)
141 /* couln't open the file */
142 return false;
145 /* Initialization */
146 memset(cue, 0, sizeof(struct cuesheet));
147 strcpy(cue->path, file);
148 cue->curr_track = cue->tracks;
150 while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
152 s = skip_whitespace(line);
154 if (!strncmp(s, "TRACK", 5))
156 cue->track_count++;
158 else if (!strncmp(s, "INDEX 01", 8))
160 s = strchr(s,' ');
161 s = skip_whitespace(s);
162 s = strchr(s,' ');
163 s = skip_whitespace(s);
164 cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
165 s = strchr(s,':') + 1;
166 cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
167 s = strchr(s,':') + 1;
168 cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
170 else if (!strncmp(s, "TITLE", 5)
171 || !strncmp(s, "PERFORMER", 9)
172 || !strncmp(s, "SONGWRITER", 10))
174 char *dest = NULL;
175 char *string = get_string(s);
176 if (!string)
177 break;
179 switch (*s)
181 case 'T': /* TITLE */
182 dest = (cue->track_count <= 0) ? cue->title :
183 cue->tracks[cue->track_count-1].title;
184 break;
186 case 'P': /* PERFORMER */
187 dest = (cue->track_count <= 0) ? cue->performer :
188 cue->tracks[cue->track_count-1].performer;
189 break;
191 case 'S': /* SONGWRITER */
192 dest = (cue->track_count <= 0) ? cue->songwriter :
193 cue->tracks[cue->track_count-1].songwriter;
194 break;
197 if (dest)
198 strncpy(dest, string, MAX_NAME);
201 close(fd);
203 /* If some songs don't have performer info, we copy the cuesheet performer */
204 int i;
205 for (i = 0; i < cue->track_count; i++)
207 if (*(cue->tracks[i].performer) == '\0')
208 strncpy(cue->tracks[i].performer, cue->performer, MAX_NAME);
210 if (*(cue->tracks[i].songwriter) == '\0')
211 strncpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME);
214 return true;
217 /* takes care of seeking to a track in a playlist
218 * returns false if audio isn't playing */
219 static bool seek(unsigned long pos)
221 if (!(audio_status() & AUDIO_STATUS_PLAY))
223 return false;
225 else
227 #if (CONFIG_CODEC == SWCODEC)
228 audio_pre_ff_rewind();
229 audio_ff_rewind(pos);
230 #else
231 audio_pause();
232 audio_ff_rewind(pos);
233 audio_resume();
234 #endif
235 return true;
239 /* returns the index of the track currently being played
240 and updates the information about the current track. */
241 int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
243 int i=0;
244 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
245 i++;
247 cue->curr_track_idx = i;
248 cue->curr_track = cue->tracks + i;
249 return i;
252 /* callback that gives list item titles for the cuesheet browser */
253 static char *list_get_name_cb(int selected_item,
254 void *data,
255 char *buffer)
257 struct cuesheet *cue = (struct cuesheet *)data;
259 if (selected_item & 1)
260 snprintf(buffer, MAX_PATH, "%s",
261 cue->tracks[selected_item/2].title);
262 else
263 snprintf(buffer, MAX_PATH, "%02d. %s", selected_item/2+1,
264 cue->tracks[selected_item/2].performer);
266 return buffer;
269 void browse_cuesheet(struct cuesheet *cue)
271 struct gui_synclist lists;
272 int action;
273 bool done = false;
274 int sel;
275 char title[MAX_PATH];
276 char cuepath[MAX_PATH];
277 struct mp3entry *id3 = audio_current_track();
279 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
280 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2);
281 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
282 gui_synclist_set_title(&lists, title, 0);
284 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
286 look_for_cuesheet_file(id3->path, cuepath);
289 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
291 gui_synclist_select_item(&lists,
292 2*cue_find_current_track(cue, id3->elapsed));
295 while (!done)
297 gui_synclist_draw(&lists);
298 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
299 if (gui_synclist_do_button(&lists,action,LIST_WRAP_UNLESS_HELD))
300 continue;
301 switch (action)
303 case ACTION_STD_OK:
304 id3 = audio_current_track();
305 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
307 look_for_cuesheet_file(id3->path, cuepath);
308 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
310 sel = gui_synclist_get_sel_pos(&lists);
311 seek(cue->tracks[sel/2].offset);
314 break;
315 case ACTION_STD_CANCEL:
316 done = true;
321 bool display_cuesheet_content(char* filename)
323 size_t bufsize = 0;
324 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
325 if (!cue || bufsize < sizeof(struct cuesheet))
326 return false;
328 if (!parse_cuesheet(filename, cue))
329 return false;
331 browse_cuesheet(cue);
332 return true;
335 /* skips backwards or forward in the current cuesheet
336 * the return value indicates whether we're still in a cusheet after skipping
337 * it also returns false if we weren't in a cuesheet.
338 * direction should be 1 or -1.
340 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
342 int track = cue_find_current_track(curr_cue, curr_pos);
344 if (direction >= 0 && track == curr_cue->track_count - 1)
346 /* we want to get out of the cuesheet */
347 return false;
349 else
351 if (!(direction <= 0 && track == 0))
352 track += direction;
354 seek(curr_cue->tracks[track].offset);
355 return true;
360 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
362 if (!cue || !cue->curr_track)
363 return;
365 struct cue_track_info *track = cue->curr_track;
367 id3->title = *track->title ? track->title : NULL;
368 id3->artist = *track->performer ? track->performer : NULL;
369 id3->composer = *track->songwriter ? track->songwriter : NULL;
370 id3->album = *cue->title ? cue->title : NULL;
372 /* if the album artist is the same as the track artist, we hide it. */
373 if (strcmp(cue->performer, track->performer))
374 id3->albumartist = *cue->performer ? cue->performer : NULL;
375 else
376 id3->albumartist = NULL;
378 int i = cue->curr_track_idx;
379 id3->tracknum = i+1;
380 if (id3->track_string)
381 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
384 #ifdef HAVE_LCD_BITMAP
385 static inline void draw_veritcal_line_mark(struct screen * screen,
386 int x, int y, int h)
388 screen->set_drawmode(DRMODE_COMPLEMENT);
389 screen->vline(x, y, y+h-1);
392 /* draw the cuesheet markers for a track of length "tracklen",
393 between (x1,y) and (x2,y) */
394 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
395 int x1, int x2, int y, int h)
397 int i,xi;
398 int w = x2 - x1;
399 for (i=1; i < curr_cue->track_count; i++)
401 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
402 draw_veritcal_line_mark(screen, xi, y, h);
405 #endif