Explicitly set the dialog's result code for TTS / Encoder windows. Fixes an issue...
[Rockbox.git] / apps / cuesheet.c
blob98262db7319e9dd2607bc4c82b0756ad79965f7d
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 <ctype.h>
24 #include <string.h>
25 #include "system.h"
26 #include "audio.h"
27 #include "kernel.h"
28 #include "logf.h"
29 #include "sprintf.h"
30 #include "misc.h"
31 #include "screens.h"
32 #include "splash.h"
33 #include "list.h"
34 #include "action.h"
35 #include "lang.h"
36 #include "debug.h"
37 #include "settings.h"
38 #include "buffer.h"
39 #include "plugin.h"
40 #include "playback.h"
41 #include "cuesheet.h"
43 #define CUE_DIR ROCKBOX_DIR "/cue"
45 struct cuesheet *curr_cue;
46 struct cuesheet *temp_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 temp_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
61 #if CONFIG_CODEC != SWCODEC
62 audio_set_cuesheet_callback(cuesheet_handler);
63 #endif
64 } else {
65 curr_cue = NULL;
66 temp_cue = NULL;
70 bool cuesheet_is_enabled(void)
72 return (curr_cue != NULL);
75 bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
77 /* DEBUGF("look for cue file\n"); */
79 char cuepath[MAX_PATH];
80 char *dot, *slash;
82 slash = strrchr(trackpath, '/');
83 if (!slash)
85 found_cue_path = NULL;
86 return false;
89 strncpy(cuepath, trackpath, MAX_PATH);
90 dot = strrchr(cuepath, '.');
91 strcpy(dot, ".cue");
93 if (!file_exists(cuepath))
95 strcpy(cuepath, CUE_DIR);
96 strcat(cuepath, slash);
97 char *dot = strrchr(cuepath, '.');
98 strcpy(dot, ".cue");
99 if (!file_exists(cuepath))
101 if (found_cue_path)
102 found_cue_path = NULL;
103 return false;
107 if (found_cue_path)
108 strncpy(found_cue_path, cuepath, MAX_PATH);
109 return true;
112 static char *skip_whitespace(char* buf)
114 char *r = buf;
115 while (*r && isspace(*r))
116 r++;
117 return r;
121 static char *get_string(const char *line)
123 char *start, *end;
125 start = strchr(line, '"');
126 if (!start)
128 start = strchr(line, ' ');
130 if (!start)
131 return NULL;
134 end = strchr(++start, '"');
135 if (end)
136 *end = '\0';
138 return start;
141 /* parse cuesheet "file" and store the information in "cue" */
142 bool parse_cuesheet(char *file, struct cuesheet *cue)
144 char line[MAX_PATH];
145 char *s;
147 DEBUGF("cue parse\n");
148 int fd = open(file,O_RDONLY);
149 if (fd < 0)
151 /* couln't open the file */
152 return false;
155 /* Initialization */
156 memset(cue, 0, sizeof(struct cuesheet));
157 strcpy(cue->path, file);
158 cue->curr_track = cue->tracks;
160 while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
162 s = skip_whitespace(line);
164 if (!strncmp(s, "TRACK", 5))
166 cue->track_count++;
168 else if (!strncmp(s, "INDEX 01", 8))
170 s = strchr(s,' ');
171 s = skip_whitespace(s);
172 s = strchr(s,' ');
173 s = skip_whitespace(s);
174 cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
175 s = strchr(s,':') + 1;
176 cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
177 s = strchr(s,':') + 1;
178 cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
180 else if (!strncmp(s, "TITLE", 5)
181 || !strncmp(s, "PERFORMER", 9)
182 || !strncmp(s, "SONGWRITER", 10))
184 char *dest = NULL;
185 char *string = get_string(s);
186 if (!string)
187 break;
189 switch (*s)
191 case 'T': /* TITLE */
192 dest = (cue->track_count <= 0) ? cue->title :
193 cue->tracks[cue->track_count-1].title;
194 break;
196 case 'P': /* PERFORMER */
197 dest = (cue->track_count <= 0) ? cue->performer :
198 cue->tracks[cue->track_count-1].performer;
199 break;
201 case 'S': /* SONGWRITER */
202 dest = (cue->track_count <= 0) ? cue->songwriter :
203 cue->tracks[cue->track_count-1].songwriter;
204 break;
207 if (dest) {
208 dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
209 *dest = '\0';
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 strncpy(cue->tracks[i].performer, cue->performer, MAX_NAME);
222 if (*(cue->tracks[i].songwriter) == '\0')
223 strncpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME);
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 snprintf(buffer, buffer_len, "%s",
274 cue->tracks[selected_item/2].title);
275 else
276 snprintf(buffer, buffer_len, "%02d. %s", selected_item/2+1,
277 cue->tracks[selected_item/2].performer);
279 return buffer;
282 void browse_cuesheet(struct cuesheet *cue)
284 struct gui_synclist lists;
285 int action;
286 bool done = false;
287 int sel;
288 char title[MAX_PATH];
289 char cuepath[MAX_PATH];
290 struct mp3entry *id3 = audio_current_track();
292 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
293 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2, NULL);
294 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
295 gui_synclist_set_title(&lists, title, 0);
297 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
299 look_for_cuesheet_file(id3->path, cuepath);
302 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
304 gui_synclist_select_item(&lists,
305 2*cue_find_current_track(cue, id3->elapsed));
308 while (!done)
310 gui_synclist_draw(&lists);
311 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
312 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
313 continue;
314 switch (action)
316 case ACTION_STD_OK:
317 id3 = audio_current_track();
318 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
320 look_for_cuesheet_file(id3->path, cuepath);
321 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
323 sel = gui_synclist_get_sel_pos(&lists);
324 seek(cue->tracks[sel/2].offset);
327 break;
328 case ACTION_STD_CANCEL:
329 done = true;
334 bool display_cuesheet_content(char* filename)
336 size_t bufsize = 0;
337 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
338 if (!cue || bufsize < sizeof(struct cuesheet))
339 return false;
341 if (!parse_cuesheet(filename, cue))
342 return false;
344 browse_cuesheet(cue);
345 return true;
348 /* skips backwards or forward in the current cuesheet
349 * the return value indicates whether we're still in a cusheet after skipping
350 * it also returns false if we weren't in a cuesheet.
351 * direction should be 1 or -1.
353 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
355 int track = cue_find_current_track(curr_cue, curr_pos);
357 if (direction >= 0 && track == curr_cue->track_count - 1)
359 /* we want to get out of the cuesheet */
360 return false;
362 else
364 if (!(direction <= 0 && track == 0))
365 track += direction;
367 seek(curr_cue->tracks[track].offset);
368 return true;
373 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
375 if (!cue || !cue->curr_track)
376 return;
378 struct cue_track_info *track = cue->curr_track;
380 id3->title = *track->title ? track->title : NULL;
381 id3->artist = *track->performer ? track->performer : NULL;
382 id3->composer = *track->songwriter ? track->songwriter : NULL;
383 id3->album = *cue->title ? cue->title : NULL;
385 /* if the album artist is the same as the track artist, we hide it. */
386 if (strcmp(cue->performer, track->performer))
387 id3->albumartist = *cue->performer ? cue->performer : NULL;
388 else
389 id3->albumartist = NULL;
391 int i = cue->curr_track_idx;
392 id3->tracknum = i+1;
393 if (id3->track_string)
394 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
397 #ifdef HAVE_LCD_BITMAP
398 static inline void draw_veritcal_line_mark(struct screen * screen,
399 int x, int y, int h)
401 screen->set_drawmode(DRMODE_COMPLEMENT);
402 screen->vline(x, y, y+h-1);
405 /* draw the cuesheet markers for a track of length "tracklen",
406 between (x1,y) and (x2,y) */
407 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
408 int x1, int x2, int y, int h)
410 int i,xi;
411 int w = x2 - x1;
412 for (i=1; i < curr_cue->track_count; i++)
414 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
415 draw_veritcal_line_mark(screen, xi, y, h);
418 #endif