Fix red
[Rockbox.git] / apps / recorder / albumart.c
blob9da5d824b5bfd3db52b4e8ad368148b28dbaa6f3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin
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 <string.h>
21 #include "sprintf.h"
22 #include "system.h"
23 #include "albumart.h"
24 #include "id3.h"
25 #include "gwps.h"
26 #include "buffering.h"
27 #include "dircache.h"
28 #include "debug.h"
29 #include "misc.h"
30 #include "settings.h"
33 /* Strip filename from a full path
35 * buf - buffer to extract directory to.
36 * buf_size - size of buffer.
37 * fullpath - fullpath to extract from.
39 * Split the directory part of the given fullpath and store it in buf
40 * (including last '/').
41 * The function return parameter is a pointer to the filename
42 * inside the given fullpath.
44 static char* strip_filename(char* buf, int buf_size, const char* fullpath)
46 char* sep;
47 int len;
49 if (!buf || buf_size <= 0 || !fullpath)
50 return NULL;
52 /* if 'fullpath' is only a filename return immediately */
53 sep = strrchr(fullpath, '/');
54 if (sep == NULL)
56 buf[0] = 0;
57 return (char*)fullpath;
60 len = MIN(sep - fullpath + 1, buf_size - 1);
61 strncpy(buf, fullpath, len);
62 buf[len] = 0;
63 return (sep + 1);
66 /* Make sure part of path only contain chars valid for a FAT32 long name.
67 * Double quotes are replaced with single quotes, other unsupported chars
68 * are replaced with an underscore.
70 * path - path to modify.
71 * offset - where in path to start checking.
72 * count - number of chars to check.
74 static void fix_path_part(char* path, int offset, int count)
76 static const char invalid_chars[] = "*/:<>?\\|";
77 int i;
79 path += offset;
81 for (i = 0; i <= count; i++, path++)
83 if (*path == 0)
84 return;
85 if (*path == '"')
86 *path = '\'';
87 else if (strchr(invalid_chars, *path))
88 *path = '_';
92 /* Look for the first matching album art bitmap in the following list:
93 * ./<trackname><size>.bmp
94 * ./<albumname><size>.bmp
95 * ./cover<size>.bmp
96 * ../<albumname><size>.bmp
97 * ../cover<size>.bmp
98 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.bmp
99 * <size> is the value of the size_string parameter, <trackname> and
100 * <albumname> are read from the ID3 metadata.
101 * If a matching bitmap is found, its filename is stored in buf.
102 * Return value is true if a bitmap was found, false otherwise.
104 bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
105 char *buf, int buflen)
107 char path[MAX_PATH + 1];
108 char dir[MAX_PATH + 1];
109 bool found = false;
110 const char *trackname;
111 int dirlen;
112 int albumlen;
114 if (!id3 || !buf)
115 return false;
117 trackname = id3->path;
119 if (strcmp(trackname, "No file!") == 0)
120 return false;
122 strip_filename(dir, sizeof(dir), trackname);
123 dirlen = strlen(dir);
124 albumlen = id3->album ? strlen(id3->album) : 0;
126 /* the first file we look for is one specific to the track playing */
127 strip_extension(path, sizeof(path) - strlen(size_string) - 4, trackname);
128 strcat(path, size_string);
129 strcat(path, ".bmp");
130 found = file_exists(path);
131 if (!found && albumlen > 0)
133 /* if it doesn't exist,
134 * we look for a file specific to the track's album name */
135 snprintf(path, sizeof(path),
136 "%s%s%s.bmp", dir, id3->album, size_string);
137 fix_path_part(path, dirlen, albumlen);
138 found = file_exists(path);
141 if (!found)
143 /* if it still doesn't exist, we look for a generic file */
144 snprintf(path, sizeof(path),
145 "%scover%s.bmp", dir, size_string);
146 found = file_exists(path);
149 if (!found && id3->artist && id3->album)
151 /* look in the albumart subdir of .rockbox */
152 snprintf(path, sizeof(path),
153 ROCKBOX_DIR "/albumart/%s-%s%s.bmp",
154 id3->artist,
155 id3->album,
156 size_string);
157 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
158 found = file_exists(path);
161 if (!found)
163 /* if it still doesn't exist,
164 * we continue to search in the parent directory */
165 strcpy(path, dir);
166 path[dirlen - 1] = 0;
167 strip_filename(dir, sizeof(dir), path);
168 dirlen = strlen(dir);
171 /* only try parent if there is one */
172 if (dirlen > 0)
174 if (!found && albumlen > 0)
176 /* we look in the parent directory
177 * for a file specific to the track's album name */
178 snprintf(path, sizeof(path),
179 "%s%s%s.bmp", dir, id3->album, size_string);
180 fix_path_part(path, dirlen, albumlen);
181 found = file_exists(path);
184 if (!found)
186 /* if it still doesn't exist, we look in the parent directory
187 * for a generic file */
188 snprintf(path, sizeof(path),
189 "%scover%s.bmp", dir, size_string);
190 found = file_exists(path);
194 if (!found)
195 return false;
197 strncpy(buf, path, buflen);
198 DEBUGF("Album art found: %s\n", path);
199 return true;
202 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
203 * Stores the found filename in the buf parameter.
204 * Returns true if a bitmap was found, false otherwise */
205 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen)
207 if (!id3 || !buf)
208 return false;
210 char size_string[9];
211 struct wps_data *data = gui_wps[0].data;
213 if (!data)
214 return false;
216 DEBUGF("Looking for album art for %s\n", id3->path);
218 /* Write the size string, e.g. ".100x100". */
219 snprintf(size_string, sizeof(size_string), ".%dx%d",
220 data->albumart_max_width, data->albumart_max_height);
222 /* First we look for a bitmap of the right size */
223 if (search_albumart_files(id3, size_string, buf, buflen))
224 return true;
226 /* Then we look for generic bitmaps */
227 *size_string = 0;
228 return search_albumart_files(id3, size_string, buf, buflen);
231 /* Draw the album art bitmap from the given handle ID onto the given WPS.
232 Call with clear = true to clear the bitmap instead of drawing it. */
233 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
235 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
236 return;
238 struct wps_data *data = gwps->data;
240 #ifdef HAVE_REMOTE_LCD
241 /* No album art on RWPS */
242 if (data->remote_wps)
243 return;
244 #endif
246 struct bitmap *bmp;
247 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
248 return;
250 short x = data->albumart_x;
251 short y = data->albumart_y;
252 short width = bmp->width;
253 short height = bmp->height;
255 if (data->albumart_max_width > 0)
257 /* Crop if the bitmap is too wide */
258 width = MIN(bmp->width, data->albumart_max_width);
260 /* Align */
261 if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT)
262 x += data->albumart_max_width - width;
263 else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER)
264 x += (data->albumart_max_width - width) / 2;
267 if (data->albumart_max_height > 0)
269 /* Crop if the bitmap is too high */
270 height = MIN(bmp->height, data->albumart_max_height);
272 /* Align */
273 if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM)
274 y += data->albumart_max_height - height;
275 else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER)
276 y += (data->albumart_max_height - height) / 2;
279 if (!clear)
281 /* Draw the bitmap */
282 gwps->display->set_drawmode(DRMODE_FG);
283 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width,
284 x, y, width, height);
285 gwps->display->set_drawmode(DRMODE_SOLID);
287 else
289 /* Clear the bitmap */
290 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
291 gwps->display->fillrect(x, y, width, height);
292 gwps->display->set_drawmode(DRMODE_SOLID);