Alright, revert r21229 for now. Stupid me forgetting about the inclusion sequence :(
[kugel-rb.git] / apps / recorder / albumart.c
blobbba2f1b67e9f6dc096f11b4f7b067a80c4d5743e
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.h>
23 #include "sprintf.h"
24 #include "system.h"
25 #include "albumart.h"
26 #include "metadata.h"
27 #include "gwps.h"
28 #include "buffering.h"
29 #include "dircache.h"
30 #include "debug.h"
31 #include "misc.h"
32 #include "settings.h"
34 #if defined(HAVE_JPEG) || defined(PLUGIN)
35 #define USE_JPEG_COVER
36 #endif
38 /* Strip filename from a full path
40 * buf - buffer to extract directory to.
41 * buf_size - size of buffer.
42 * fullpath - fullpath to extract from.
44 * Split the directory part of the given fullpath and store it in buf
45 * (including last '/').
46 * The function return parameter is a pointer to the filename
47 * inside the given fullpath.
49 static char* strip_filename(char* buf, int buf_size, const char* fullpath)
51 char* sep;
52 int len;
54 if (!buf || buf_size <= 0 || !fullpath)
55 return NULL;
57 /* if 'fullpath' is only a filename return immediately */
58 sep = strrchr(fullpath, '/');
59 if (sep == NULL)
61 buf[0] = 0;
62 return (char*)fullpath;
65 len = MIN(sep - fullpath + 1, buf_size - 1);
66 strncpy(buf, fullpath, len);
67 buf[len] = 0;
68 return (sep + 1);
71 /* Make sure part of path only contain chars valid for a FAT32 long name.
72 * Double quotes are replaced with single quotes, other unsupported chars
73 * are replaced with an underscore.
75 * path - path to modify.
76 * offset - where in path to start checking.
77 * count - number of chars to check.
79 static void fix_path_part(char* path, int offset, int count)
81 static const char invalid_chars[] = "*/:<>?\\|";
82 int i;
84 path += offset;
86 for (i = 0; i <= count; i++, path++)
88 if (*path == 0)
89 return;
90 if (*path == '"')
91 *path = '\'';
92 else if (strchr(invalid_chars, *path))
93 *path = '_';
97 #ifdef USE_JPEG_COVER
98 static const char * extensions[] = { "jpeg", "jpg", "bmp" };
99 static int extension_lens[] = { 4, 3, 3 };
100 /* Try checking for several file extensions, return true if a file is found and
101 * leaving the path modified to include the matching extension.
103 static bool try_exts(char *path, int len)
105 int i;
106 for (i = 0; i < 3; i++)
108 if (extension_lens[i] + len > MAX_PATH)
109 continue;
110 strcpy(path + len, extensions[i]);
111 if (file_exists(path))
112 return true;
114 return false;
116 #define EXT
117 #else
118 #define EXT "bmp"
119 #define try_exts(path, len) file_exists(path)
120 #endif
122 /* Look for the first matching album art bitmap in the following list:
123 * ./<trackname><size>.{jpeg,jpg,bmp}
124 * ./<albumname><size>.{jpeg,jpg,bmp}
125 * ./cover<size>.bmp
126 * ../<albumname><size>.{jpeg,jpg,bmp}
127 * ../cover<size>.{jpeg,jpg,bmp}
128 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
129 * <size> is the value of the size_string parameter, <trackname> and
130 * <albumname> are read from the ID3 metadata.
131 * If a matching bitmap is found, its filename is stored in buf.
132 * Return value is true if a bitmap was found, false otherwise.
134 * If the first symbol in size_string is a colon (e.g. ":100x100")
135 * then the colon is skipped ("100x100" will be used) and the track
136 * specific image (./<trackname><size>.bmp) is tried last instead of first.
138 bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
139 char *buf, int buflen)
141 char path[MAX_PATH + 1];
142 char dir[MAX_PATH + 1];
143 bool found = false;
144 int track_first = 1;
145 int pass;
146 const char *trackname;
147 const char *artist;
148 int dirlen;
149 int albumlen;
150 int pathlen;
152 if (!id3 || !buf)
153 return false;
155 trackname = id3->path;
157 if (strcmp(trackname, "No file!") == 0)
158 return false;
160 if (*size_string == ':')
162 size_string++;
163 track_first = 0;
166 strip_filename(dir, sizeof(dir), trackname);
167 dirlen = strlen(dir);
168 albumlen = id3->album ? strlen(id3->album) : 0;
170 for(pass = 0; pass < 2 - track_first; pass++)
172 if (track_first || pass)
174 /* the first file we look for is one specific to the
175 current track */
176 strip_extension(path, sizeof(path) - strlen(size_string) - 4,
177 trackname);
178 strcat(path, size_string);
179 strcat(path, "." EXT);
180 #ifdef USE_JPEG_COVER
181 pathlen = strlen(path);
182 #endif
183 found = try_exts(path, pathlen);
185 if (pass)
186 break;
187 if (!found && albumlen > 0)
189 /* if it doesn't exist,
190 * we look for a file specific to the track's album name */
191 pathlen = snprintf(path, sizeof(path),
192 "%s%s%s." EXT, dir, id3->album, size_string);
193 fix_path_part(path, dirlen, albumlen);
194 found = try_exts(path, pathlen);
197 if (!found)
199 /* if it still doesn't exist, we look for a generic file */
200 pathlen = snprintf(path, sizeof(path),
201 "%scover%s." EXT, dir, size_string);
202 found = try_exts(path, pathlen);
205 #ifdef USE_JPEG_COVER
206 if (!found && !*size_string)
208 snprintf (path, sizeof(path), "%sfolder.jpg", dir);
209 found = file_exists(path);
211 #endif
213 artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
215 if (!found && artist && id3->album)
217 /* look in the albumart subdir of .rockbox */
218 pathlen = snprintf(path, sizeof(path),
219 ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
220 artist,
221 id3->album,
222 size_string);
223 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
224 found = try_exts(path, pathlen);
227 if (!found)
229 /* if it still doesn't exist,
230 * we continue to search in the parent directory */
231 strcpy(path, dir);
232 path[dirlen - 1] = 0;
233 strip_filename(dir, sizeof(dir), path);
234 dirlen = strlen(dir);
237 /* only try parent if there is one */
238 if (dirlen > 0)
240 if (!found && albumlen > 0)
242 /* we look in the parent directory
243 * for a file specific to the track's album name */
244 pathlen = snprintf(path, sizeof(path),
245 "%s%s%s." EXT, dir, id3->album, size_string);
246 fix_path_part(path, dirlen, albumlen);
247 found = try_exts(path, pathlen);
250 if (!found)
252 /* if it still doesn't exist, we look in the parent directory
253 * for a generic file */
254 pathlen = snprintf(path, sizeof(path),
255 "%scover%s." EXT, dir, size_string);
256 found = try_exts(path, pathlen);
259 if (found)
260 break;
263 if (!found)
264 return false;
266 strncpy(buf, path, buflen);
267 DEBUGF("Album art found: %s\n", path);
268 return true;
271 #ifndef PLUGIN
272 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
273 * Stores the found filename in the buf parameter.
274 * Returns true if a bitmap was found, false otherwise */
275 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen)
277 if (!id3 || !buf)
278 return false;
280 char size_string[9];
281 struct wps_data *data = gui_wps[0].data;
283 if (!data)
284 return false;
286 DEBUGF("Looking for album art for %s\n", id3->path);
288 /* Write the size string, e.g. ".100x100". */
289 snprintf(size_string, sizeof(size_string), ".%dx%d",
290 data->albumart_max_width, data->albumart_max_height);
292 /* First we look for a bitmap of the right size */
293 if (search_albumart_files(id3, size_string, buf, buflen))
294 return true;
296 /* Then we look for generic bitmaps */
297 *size_string = 0;
298 return search_albumart_files(id3, size_string, buf, buflen);
301 /* Draw the album art bitmap from the given handle ID onto the given WPS.
302 Call with clear = true to clear the bitmap instead of drawing it. */
303 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
305 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
306 return;
308 struct wps_data *data = gwps->data;
310 #ifdef HAVE_REMOTE_LCD
311 /* No album art on RWPS */
312 if (data->remote_wps)
313 return;
314 #endif
316 struct bitmap *bmp;
317 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
318 return;
320 short x = data->albumart_x;
321 short y = data->albumart_y;
322 short width = bmp->width;
323 short height = bmp->height;
325 if (data->albumart_max_width > 0)
327 /* Crop if the bitmap is too wide */
328 width = MIN(bmp->width, data->albumart_max_width);
330 /* Align */
331 if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT)
332 x += data->albumart_max_width - width;
333 else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER)
334 x += (data->albumart_max_width - width) / 2;
337 if (data->albumart_max_height > 0)
339 /* Crop if the bitmap is too high */
340 height = MIN(bmp->height, data->albumart_max_height);
342 /* Align */
343 if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM)
344 y += data->albumart_max_height - height;
345 else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER)
346 y += (data->albumart_max_height - height) / 2;
349 if (!clear)
351 /* Draw the bitmap */
352 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width,
353 x, y, width, height);
354 #ifdef HAVE_LCD_INVERT
355 if (global_settings.invert) {
356 gwps->display->set_drawmode(DRMODE_COMPLEMENT);
357 gwps->display->fillrect(x, y, width, height);
358 gwps->display->set_drawmode(DRMODE_SOLID);
360 #endif
362 else
364 /* Clear the bitmap */
365 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
366 gwps->display->fillrect(x, y, width, height);
367 gwps->display->set_drawmode(DRMODE_SOLID);
371 void get_albumart_size(struct bitmap *bmp)
373 /* FIXME: What should we do with albumart on remote? */
374 struct wps_data *data = gui_wps[0].data;
376 bmp->width = data->albumart_max_width;
377 bmp->height = data->albumart_max_height;
379 #endif /* PLUGIN */