set YOFS to 0 for portrait LCDs.
[kugel-rb.git] / apps / cuesheet.c
blobfa1d93f33412ad0c5417abe69ed2da3dd6b2b198
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin, Jonathan Gordon
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 <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include "system.h"
28 #include "audio.h"
29 #include "kernel.h"
30 #include "logf.h"
31 #include "sprintf.h"
32 #include "misc.h"
33 #include "screens.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;
48 #if CONFIG_CODEC != SWCODEC
49 /* special trickery because the hwcodec playback engine is in firmware/ */
50 static bool cuesheet_handler(const char *filename)
52 return cuesheet_is_enabled() && look_for_cuesheet_file(filename, NULL);
54 #endif
56 void cuesheet_init(void)
58 if (global_settings.cuesheet) {
59 curr_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
60 #if CONFIG_CODEC != SWCODEC
61 audio_set_cuesheet_callback(cuesheet_handler);
62 #endif
63 } else {
64 curr_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"); */
77 char cuepath[MAX_PATH];
78 char *dot, *slash;
80 slash = strrchr(trackpath, '/');
81 if (!slash)
83 found_cue_path = NULL;
84 return false;
87 strlcpy(cuepath, trackpath, MAX_PATH);
88 dot = strrchr(cuepath, '.');
89 strcpy(dot, ".cue");
91 if (!file_exists(cuepath))
93 strcpy(cuepath, CUE_DIR);
94 strcat(cuepath, slash);
95 char *dot = strrchr(cuepath, '.');
96 strcpy(dot, ".cue");
97 if (!file_exists(cuepath))
99 if (found_cue_path)
100 found_cue_path = NULL;
101 return false;
105 if (found_cue_path)
106 strlcpy(found_cue_path, cuepath, MAX_PATH);
107 return true;
110 static char *get_string(const char *line)
112 char *start, *end;
114 start = strchr(line, '"');
115 if (!start)
117 start = strchr(line, ' ');
119 if (!start)
120 return NULL;
123 end = strchr(++start, '"');
124 if (end)
125 *end = '\0';
127 return start;
130 /* parse cuesheet "file" and store the information in "cue" */
131 bool parse_cuesheet(char *file, struct cuesheet *cue)
133 char line[MAX_PATH];
134 char *s;
135 bool utf8 = false;
137 DEBUGF("cue parse\n");
138 int fd = open_utf8(file,O_RDONLY);
139 if (fd < 0)
141 /* couln't open the file */
142 return false;
144 if(lseek(fd, 0, SEEK_CUR) > 0)
145 utf8 = true;
147 /* Initialization */
148 memset(cue, 0, sizeof(struct cuesheet));
149 strcpy(cue->path, file);
150 cue->curr_track = cue->tracks;
152 while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
154 s = skip_whitespace(line);
156 if (!strncmp(s, "TRACK", 5))
158 cue->track_count++;
160 else if (!strncmp(s, "INDEX 01", 8))
162 s = strchr(s,' ');
163 s = skip_whitespace(s);
164 s = strchr(s,' ');
165 s = skip_whitespace(s);
166 cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
167 s = strchr(s,':') + 1;
168 cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
169 s = strchr(s,':') + 1;
170 cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
172 else if (!strncmp(s, "TITLE", 5)
173 || !strncmp(s, "PERFORMER", 9)
174 || !strncmp(s, "SONGWRITER", 10))
176 char *dest = NULL;
177 char *string = get_string(s);
178 if (!string)
179 break;
181 switch (*s)
183 case 'T': /* TITLE */
184 dest = (cue->track_count <= 0) ? cue->title :
185 cue->tracks[cue->track_count-1].title;
186 break;
188 case 'P': /* PERFORMER */
189 dest = (cue->track_count <= 0) ? cue->performer :
190 cue->tracks[cue->track_count-1].performer;
191 break;
193 case 'S': /* SONGWRITER */
194 dest = (cue->track_count <= 0) ? cue->songwriter :
195 cue->tracks[cue->track_count-1].songwriter;
196 break;
199 if (dest)
201 if (!utf8)
203 dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
204 *dest = '\0';
206 else
208 strlcpy(dest, string, MAX_NAME*3 + 1);
213 close(fd);
215 /* If some songs don't have performer info, we copy the cuesheet performer */
216 int i;
217 for (i = 0; i < cue->track_count; i++)
219 if (*(cue->tracks[i].performer) == '\0')
220 strlcpy(cue->tracks[i].performer, cue->performer, MAX_NAME*3);
222 if (*(cue->tracks[i].songwriter) == '\0')
223 strlcpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME*3);
226 return true;
229 /* takes care of seeking to a track in a playlist
230 * returns false if audio isn't playing */
231 static bool seek(unsigned long pos)
233 if (!(audio_status() & AUDIO_STATUS_PLAY))
235 return false;
237 else
239 #if (CONFIG_CODEC == SWCODEC)
240 audio_pre_ff_rewind();
241 audio_ff_rewind(pos);
242 #else
243 audio_pause();
244 audio_ff_rewind(pos);
245 audio_resume();
246 #endif
247 return true;
251 /* returns the index of the track currently being played
252 and updates the information about the current track. */
253 int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
255 int i=0;
256 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
257 i++;
259 cue->curr_track_idx = i;
260 cue->curr_track = cue->tracks + i;
261 return i;
264 /* callback that gives list item titles for the cuesheet browser */
265 static char *list_get_name_cb(int selected_item,
266 void *data,
267 char *buffer,
268 size_t buffer_len)
270 struct cuesheet *cue = (struct cuesheet *)data;
272 if (selected_item & 1)
273 strlcpy(buffer, cue->tracks[selected_item/2].title, buffer_len);
274 else
275 snprintf(buffer, buffer_len, "%02d. %s", selected_item/2+1,
276 cue->tracks[selected_item/2].performer);
278 return buffer;
281 void browse_cuesheet(struct cuesheet *cue)
283 struct gui_synclist lists;
284 int action;
285 bool done = false;
286 int sel;
287 char title[MAX_PATH];
288 char cuepath[MAX_PATH];
289 struct mp3entry *id3 = audio_current_track();
291 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
292 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2, NULL);
293 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
294 gui_synclist_set_title(&lists, title, 0);
296 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
298 look_for_cuesheet_file(id3->path, cuepath);
301 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
303 gui_synclist_select_item(&lists,
304 2*cue_find_current_track(cue, id3->elapsed));
307 while (!done)
309 gui_synclist_draw(&lists);
310 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
311 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
312 continue;
313 switch (action)
315 case ACTION_STD_OK:
316 id3 = audio_current_track();
317 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
319 look_for_cuesheet_file(id3->path, cuepath);
320 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
322 sel = gui_synclist_get_sel_pos(&lists);
323 seek(cue->tracks[sel/2].offset);
326 break;
327 case ACTION_STD_CANCEL:
328 done = true;
333 bool display_cuesheet_content(char* filename)
335 size_t bufsize = 0;
336 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
337 if (!cue || bufsize < sizeof(struct cuesheet))
338 return false;
340 if (!parse_cuesheet(filename, cue))
341 return false;
343 browse_cuesheet(cue);
344 return true;
347 /* skips backwards or forward in the current cuesheet
348 * the return value indicates whether we're still in a cusheet after skipping
349 * it also returns false if we weren't in a cuesheet.
350 * direction should be 1 or -1.
352 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
354 int track = cue_find_current_track(curr_cue, curr_pos);
356 if (direction >= 0 && track == curr_cue->track_count - 1)
358 /* we want to get out of the cuesheet */
359 return false;
361 else
363 if (!(direction <= 0 && track == 0))
364 track += direction;
366 seek(curr_cue->tracks[track].offset);
367 return true;
372 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
374 if (!cue || !cue->curr_track)
375 return;
377 struct cue_track_info *track = cue->curr_track;
379 id3->title = *track->title ? track->title : NULL;
380 id3->artist = *track->performer ? track->performer : NULL;
381 id3->composer = *track->songwriter ? track->songwriter : NULL;
382 id3->album = *cue->title ? cue->title : NULL;
384 /* if the album artist is the same as the track artist, we hide it. */
385 if (strcmp(cue->performer, track->performer))
386 id3->albumartist = *cue->performer ? cue->performer : NULL;
387 else
388 id3->albumartist = NULL;
390 int i = cue->curr_track_idx;
391 id3->tracknum = i+1;
392 if (id3->track_string)
393 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
396 #ifdef HAVE_LCD_BITMAP
397 static inline void draw_veritcal_line_mark(struct screen * screen,
398 int x, int y, int h)
400 screen->set_drawmode(DRMODE_COMPLEMENT);
401 screen->vline(x, y, y+h-1);
404 /* draw the cuesheet markers for a track of length "tracklen",
405 between (x1,y) and (x2,y) */
406 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
407 int x1, int x2, int y, int h)
409 int i,xi;
410 int w = x2 - x1;
411 for (i=1; i < curr_cue->track_count; i++)
413 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
414 draw_veritcal_line_mark(screen, xi, y, h);
417 #endif