Merge branch 'master' into gsoc-dir-split
[kugel-rb.git] / apps / recorder / albumart.c
blob5eca71354205b02721e8ec989cc7178e104b788e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin
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 "string-extra.h"
23 #include "system.h"
24 #include "albumart.h"
25 #include "metadata.h"
26 #include "buffering.h"
27 #include "dircache.h"
28 #include "misc.h"
29 #include "settings.h"
30 #include "wps.h"
32 /* Define LOGF_ENABLE to enable logf output in this file */
33 /*#define LOGF_ENABLE*/
34 #include "logf.h"
36 #if defined(HAVE_JPEG) || defined(PLUGIN)
37 #define USE_JPEG_COVER
38 #endif
40 /* Strip filename from a full path
42 * buf - buffer to extract directory to.
43 * buf_size - size of buffer.
44 * fullpath - fullpath to extract from.
46 * Split the directory part of the given fullpath and store it in buf
47 * (including last '/').
48 * The function return parameter is a pointer to the filename
49 * inside the given fullpath.
51 static char* strip_filename(char* buf, int buf_size, const char* fullpath)
53 char* sep;
54 int len;
56 if (!buf || buf_size <= 0 || !fullpath)
57 return NULL;
59 /* if 'fullpath' is only a filename return immediately */
60 sep = strrchr(fullpath, '/');
61 if (sep == NULL)
63 buf[0] = 0;
64 return (char*)fullpath;
67 len = MIN(sep - fullpath + 1, buf_size - 1);
68 strlcpy(buf, fullpath, len + 1);
69 return (sep + 1);
72 /* Make sure part of path only contain chars valid for a FAT32 long name.
73 * Double quotes are replaced with single quotes, other unsupported chars
74 * are replaced with an underscore.
76 * path - path to modify.
77 * offset - where in path to start checking.
78 * count - number of chars to check.
80 static void fix_path_part(char* path, int offset, int count)
82 static const char invalid_chars[] = "*/:<>?\\|";
83 int i;
85 path += offset;
87 for (i = 0; i <= count; i++, path++)
89 if (*path == 0)
90 return;
91 if (*path == '"')
92 *path = '\'';
93 else if (strchr(invalid_chars, *path))
94 *path = '_';
98 #ifdef USE_JPEG_COVER
99 static const char * extensions[] = { "jpeg", "jpg", "bmp" };
100 static int extension_lens[] = { 4, 3, 3 };
101 /* Try checking for several file extensions, return true if a file is found and
102 * leaving the path modified to include the matching extension.
104 static bool try_exts(char *path, int len)
106 int i;
107 for (i = 0; i < 3; i++)
109 if (extension_lens[i] + len > MAX_PATH)
110 continue;
111 strcpy(path + len, extensions[i]);
112 if (file_exists(path))
113 return true;
115 return false;
117 #define EXT
118 #else
119 #define EXT "bmp"
120 #define try_exts(path, len) file_exists(path)
121 #endif
123 /* Look for the first matching album art bitmap in the following list:
124 * ./<trackname><size>.{jpeg,jpg,bmp}
125 * ./<albumname><size>.{jpeg,jpg,bmp}
126 * ./cover<size>.bmp
127 * ../<albumname><size>.{jpeg,jpg,bmp}
128 * ../cover<size>.{jpeg,jpg,bmp}
129 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
130 * <size> is the value of the size_string parameter, <trackname> and
131 * <albumname> are read from the ID3 metadata.
132 * If a matching bitmap is found, its filename is stored in buf.
133 * Return value is true if a bitmap was found, false otherwise.
135 * If the first symbol in size_string is a colon (e.g. ":100x100")
136 * then the colon is skipped ("100x100" will be used) and the track
137 * specific image (./<trackname><size>.bmp) is tried last instead of first.
139 bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
140 char *buf, int buflen)
142 char path[MAX_PATH + 1];
143 char dir[MAX_PATH + 1];
144 bool found = false;
145 int track_first = 1;
146 int pass;
147 const char *trackname;
148 const char *artist;
149 int dirlen;
150 int albumlen;
151 int pathlen;
153 if (!id3 || !buf)
154 return false;
156 trackname = id3->path;
158 if (strcmp(trackname, "No file!") == 0)
159 return false;
161 if (*size_string == ':')
163 size_string++;
164 track_first = 0;
167 strip_filename(dir, sizeof(dir), trackname);
168 dirlen = strlen(dir);
169 albumlen = id3->album ? strlen(id3->album) : 0;
171 for(pass = 0; pass < 2 - track_first; pass++)
173 if (track_first || pass)
175 /* the first file we look for is one specific to the
176 current track */
177 strip_extension(path, sizeof(path) - strlen(size_string) - 4,
178 trackname);
179 strcat(path, size_string);
180 strcat(path, "." EXT);
181 #ifdef USE_JPEG_COVER
182 pathlen = strlen(path);
183 #endif
184 found = try_exts(path, pathlen);
186 if (pass)
187 break;
188 if (!found && albumlen > 0)
190 /* if it doesn't exist,
191 * we look for a file specific to the track's album name */
192 pathlen = snprintf(path, sizeof(path),
193 "%s%s%s." EXT, dir, id3->album, size_string);
194 fix_path_part(path, dirlen, albumlen);
195 found = try_exts(path, pathlen);
198 if (!found)
200 /* if it still doesn't exist, we look for a generic file */
201 pathlen = snprintf(path, sizeof(path),
202 "%scover%s." EXT, dir, size_string);
203 found = try_exts(path, pathlen);
206 #ifdef USE_JPEG_COVER
207 if (!found && !*size_string)
209 snprintf (path, sizeof(path), "%sfolder.jpg", dir);
210 found = file_exists(path);
212 #endif
214 artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
216 if (!found && artist && id3->album)
218 /* look in the albumart subdir of .rockbox */
219 pathlen = snprintf(path, sizeof(path),
220 ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
221 artist,
222 id3->album,
223 size_string);
224 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
225 found = try_exts(path, pathlen);
228 if (!found)
230 /* if it still doesn't exist,
231 * we continue to search in the parent directory */
232 strcpy(path, dir);
233 path[dirlen - 1] = 0;
234 strip_filename(dir, sizeof(dir), path);
235 dirlen = strlen(dir);
238 /* only try parent if there is one */
239 if (dirlen > 0)
241 if (!found && albumlen > 0)
243 /* we look in the parent directory
244 * for a file specific to the track's album name */
245 pathlen = snprintf(path, sizeof(path),
246 "%s%s%s." EXT, dir, id3->album, size_string);
247 fix_path_part(path, dirlen, albumlen);
248 found = try_exts(path, pathlen);
251 if (!found)
253 /* if it still doesn't exist, we look in the parent directory
254 * for a generic file */
255 pathlen = snprintf(path, sizeof(path),
256 "%scover%s." EXT, dir, size_string);
257 found = try_exts(path, pathlen);
260 if (found)
261 break;
264 if (!found)
265 return false;
267 strlcpy(buf, path, buflen);
268 logf("Album art found: %s", path);
269 return true;
272 #ifndef PLUGIN
273 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
274 * Stores the found filename in the buf parameter.
275 * Returns true if a bitmap was found, false otherwise */
276 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen,
277 struct dim *dim)
279 if (!id3 || !buf)
280 return false;
282 char size_string[9];
283 logf("Looking for album art for %s", id3->path);
285 /* Write the size string, e.g. ".100x100". */
286 snprintf(size_string, sizeof(size_string), ".%dx%d",
287 dim->width, dim->height);
289 /* First we look for a bitmap of the right size */
290 if (search_albumart_files(id3, size_string, buf, buflen))
291 return true;
293 /* Then we look for generic bitmaps */
294 *size_string = 0;
295 return search_albumart_files(id3, size_string, buf, buflen);
298 /* Draw the album art bitmap from the given handle ID onto the given WPS.
299 Call with clear = true to clear the bitmap instead of drawing it. */
300 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
302 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
303 return;
305 struct wps_data *data = gwps->data;
306 struct skin_albumart *aa = data->albumart;
308 if (!aa)
309 return;
311 struct bitmap *bmp;
312 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
313 return;
315 short x = aa->x;
316 short y = aa->y;
317 short width = bmp->width;
318 short height = bmp->height;
320 if (aa->width > 0)
322 /* Crop if the bitmap is too wide */
323 width = MIN(bmp->width, aa->width);
325 /* Align */
326 if (aa->xalign & WPS_ALBUMART_ALIGN_RIGHT)
327 x += aa->width - width;
328 else if (aa->xalign & WPS_ALBUMART_ALIGN_CENTER)
329 x += (aa->width - width) / 2;
332 if (aa->height > 0)
334 /* Crop if the bitmap is too high */
335 height = MIN(bmp->height, aa->height);
337 /* Align */
338 if (aa->yalign & WPS_ALBUMART_ALIGN_BOTTOM)
339 y += aa->height - height;
340 else if (aa->yalign & WPS_ALBUMART_ALIGN_CENTER)
341 y += (aa->height - height) / 2;
344 if (!clear)
346 /* Draw the bitmap */
347 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0,
348 STRIDE(gwps->display->screen_type,
349 bmp->width, bmp->height),
350 x, y, width, height);
351 #ifdef HAVE_LCD_INVERT
352 if (global_settings.invert) {
353 gwps->display->set_drawmode(DRMODE_COMPLEMENT);
354 gwps->display->fillrect(x, y, width, height);
355 gwps->display->set_drawmode(DRMODE_SOLID);
357 #endif
359 else
361 /* Clear the bitmap */
362 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
363 gwps->display->fillrect(x, y, width, height);
364 gwps->display->set_drawmode(DRMODE_SOLID);
368 #endif /* PLUGIN */