fix red
[kugel-rb.git] / apps / recorder / albumart.c
blob30a4e0c49f257a2f0ebf80b366b5d6c85a02091e
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"
35 /* Strip filename from a full path
37 * buf - buffer to extract directory to.
38 * buf_size - size of buffer.
39 * fullpath - fullpath to extract from.
41 * Split the directory part of the given fullpath and store it in buf
42 * (including last '/').
43 * The function return parameter is a pointer to the filename
44 * inside the given fullpath.
46 static char* strip_filename(char* buf, int buf_size, const char* fullpath)
48 char* sep;
49 int len;
51 if (!buf || buf_size <= 0 || !fullpath)
52 return NULL;
54 /* if 'fullpath' is only a filename return immediately */
55 sep = strrchr(fullpath, '/');
56 if (sep == NULL)
58 buf[0] = 0;
59 return (char*)fullpath;
62 len = MIN(sep - fullpath + 1, buf_size - 1);
63 strncpy(buf, fullpath, len);
64 buf[len] = 0;
65 return (sep + 1);
68 /* Make sure part of path only contain chars valid for a FAT32 long name.
69 * Double quotes are replaced with single quotes, other unsupported chars
70 * are replaced with an underscore.
72 * path - path to modify.
73 * offset - where in path to start checking.
74 * count - number of chars to check.
76 static void fix_path_part(char* path, int offset, int count)
78 static const char invalid_chars[] = "*/:<>?\\|";
79 int i;
81 path += offset;
83 for (i = 0; i <= count; i++, path++)
85 if (*path == 0)
86 return;
87 if (*path == '"')
88 *path = '\'';
89 else if (strchr(invalid_chars, *path))
90 *path = '_';
94 /* Look for the first matching album art bitmap in the following list:
95 * ./<trackname><size>.bmp
96 * ./<albumname><size>.bmp
97 * ./cover<size>.bmp
98 * ../<albumname><size>.bmp
99 * ../cover<size>.bmp
100 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.bmp
101 * <size> is the value of the size_string parameter, <trackname> and
102 * <albumname> are read from the ID3 metadata.
103 * If a matching bitmap is found, its filename is stored in buf.
104 * Return value is true if a bitmap was found, false otherwise.
106 bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
107 char *buf, int buflen)
109 char path[MAX_PATH + 1];
110 char dir[MAX_PATH + 1];
111 bool found = false;
112 const char *trackname;
113 const char *artist;
114 int dirlen;
115 int albumlen;
117 if (!id3 || !buf)
118 return false;
120 trackname = id3->path;
122 if (strcmp(trackname, "No file!") == 0)
123 return false;
125 strip_filename(dir, sizeof(dir), trackname);
126 dirlen = strlen(dir);
127 albumlen = id3->album ? strlen(id3->album) : 0;
129 /* the first file we look for is one specific to the track playing */
130 strip_extension(path, sizeof(path) - strlen(size_string) - 4, trackname);
131 strcat(path, size_string);
132 strcat(path, ".bmp");
133 found = file_exists(path);
134 if (!found && albumlen > 0)
136 /* if it doesn't exist,
137 * we look for a file specific to the track's album name */
138 snprintf(path, sizeof(path),
139 "%s%s%s.bmp", dir, id3->album, size_string);
140 fix_path_part(path, dirlen, albumlen);
141 found = file_exists(path);
144 if (!found)
146 /* if it still doesn't exist, we look for a generic file */
147 snprintf(path, sizeof(path),
148 "%scover%s.bmp", dir, size_string);
149 found = file_exists(path);
152 artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
154 if (!found && artist && id3->album)
156 /* look in the albumart subdir of .rockbox */
157 snprintf(path, sizeof(path),
158 ROCKBOX_DIR "/albumart/%s-%s%s.bmp",
159 artist,
160 id3->album,
161 size_string);
162 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
163 found = file_exists(path);
166 if (!found)
168 /* if it still doesn't exist,
169 * we continue to search in the parent directory */
170 strcpy(path, dir);
171 path[dirlen - 1] = 0;
172 strip_filename(dir, sizeof(dir), path);
173 dirlen = strlen(dir);
176 /* only try parent if there is one */
177 if (dirlen > 0)
179 if (!found && albumlen > 0)
181 /* we look in the parent directory
182 * for a file specific to the track's album name */
183 snprintf(path, sizeof(path),
184 "%s%s%s.bmp", dir, id3->album, size_string);
185 fix_path_part(path, dirlen, albumlen);
186 found = file_exists(path);
189 if (!found)
191 /* if it still doesn't exist, we look in the parent directory
192 * for a generic file */
193 snprintf(path, sizeof(path),
194 "%scover%s.bmp", dir, size_string);
195 found = file_exists(path);
199 if (!found)
200 return false;
202 strncpy(buf, path, buflen);
203 DEBUGF("Album art found: %s\n", path);
204 return true;
207 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
208 * Stores the found filename in the buf parameter.
209 * Returns true if a bitmap was found, false otherwise */
210 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen)
212 if (!id3 || !buf)
213 return false;
215 char size_string[9];
216 struct wps_data *data = gui_wps[0].data;
218 if (!data)
219 return false;
221 DEBUGF("Looking for album art for %s\n", id3->path);
223 /* Write the size string, e.g. ".100x100". */
224 snprintf(size_string, sizeof(size_string), ".%dx%d",
225 data->albumart_max_width, data->albumart_max_height);
227 /* First we look for a bitmap of the right size */
228 if (search_albumart_files(id3, size_string, buf, buflen))
229 return true;
231 /* Then we look for generic bitmaps */
232 *size_string = 0;
233 return search_albumart_files(id3, size_string, buf, buflen);
236 /* Draw the album art bitmap from the given handle ID onto the given WPS.
237 Call with clear = true to clear the bitmap instead of drawing it. */
238 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
240 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
241 return;
243 struct wps_data *data = gwps->data;
245 #ifdef HAVE_REMOTE_LCD
246 /* No album art on RWPS */
247 if (data->remote_wps)
248 return;
249 #endif
251 struct bitmap *bmp;
252 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
253 return;
255 short x = data->albumart_x;
256 short y = data->albumart_y;
257 short width = bmp->width;
258 short height = bmp->height;
260 if (data->albumart_max_width > 0)
262 /* Crop if the bitmap is too wide */
263 width = MIN(bmp->width, data->albumart_max_width);
265 /* Align */
266 if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT)
267 x += data->albumart_max_width - width;
268 else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER)
269 x += (data->albumart_max_width - width) / 2;
272 if (data->albumart_max_height > 0)
274 /* Crop if the bitmap is too high */
275 height = MIN(bmp->height, data->albumart_max_height);
277 /* Align */
278 if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM)
279 y += data->albumart_max_height - height;
280 else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER)
281 y += (data->albumart_max_height - height) / 2;
284 if (!clear)
286 /* Draw the bitmap */
287 gwps->display->set_drawmode(DRMODE_FG);
288 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width,
289 x, y, width, height);
290 gwps->display->set_drawmode(DRMODE_SOLID);
292 else
294 /* Clear the bitmap */
295 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
296 gwps->display->fillrect(x, y, width, height);
297 gwps->display->set_drawmode(DRMODE_SOLID);