Redo my previous segfault fix in a better way.
[Rockbox.git] / apps / recorder / albumart.c
blob13969d77e5e5e438c4ccba00879a229f687709e1
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;
118 strip_filename(dir, sizeof(dir), trackname);
119 dirlen = strlen(dir);
120 albumlen = id3->album ? strlen(id3->album) : 0;
122 /* the first file we look for is one specific to the track playing */
123 strip_extension(trackname, path);
124 strcat(path, size_string);
125 strcat(path, ".bmp");
126 found = file_exists(path);
127 if (!found && albumlen > 0)
129 /* if it doesn't exist,
130 * we look for a file specific to the track's album name */
131 snprintf(path, sizeof(path),
132 "%s%s%s.bmp", dir, id3->album, size_string);
133 fix_path_part(path, dirlen, albumlen);
134 found = file_exists(path);
137 if (!found)
139 /* if it still doesn't exist, we look for a generic file */
140 snprintf(path, sizeof(path),
141 "%scover%s.bmp", dir, size_string);
142 found = file_exists(path);
145 if (!found)
147 /* look in the albumart subdir of .rockbox */
148 snprintf(path, sizeof(path),
149 ROCKBOX_DIR "/albumart/%s-%s%s.bmp",
150 id3->artist,
151 id3->album,
152 size_string);
153 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
154 found = file_exists(path);
157 if (!found)
159 /* if it still doesn't exist,
160 * we continue to search in the parent directory */
161 strcpy(path, dir);
162 path[dirlen - 1] = 0;
163 strip_filename(dir, sizeof(dir), path);
164 dirlen = strlen(dir);
167 /* only try parent if there is one */
168 if (dirlen > 0)
170 if (!found && albumlen > 0)
172 /* we look in the parent directory
173 * for a file specific to the track's album name */
174 snprintf(path, sizeof(path),
175 "%s%s%s.bmp", dir, id3->album, size_string);
176 fix_path_part(path, dirlen, albumlen);
177 found = file_exists(path);
180 if (!found)
182 /* if it still doesn't exist, we look in the parent directory
183 * for a generic file */
184 snprintf(path, sizeof(path),
185 "%scover%s.bmp", dir, size_string);
186 found = file_exists(path);
190 if (!found)
191 return false;
193 strncpy(buf, path, buflen);
194 DEBUGF("Album art found: %s\n", path);
195 return true;
198 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
199 * Stores the found filename in the buf parameter.
200 * Returns true if a bitmap was found, false otherwise */
201 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen)
203 if (!id3 || !buf)
204 return false;
206 char size_string[9];
207 struct wps_data *data = gui_wps[0].data;
209 if (!data)
210 return false;
212 DEBUGF("Looking for album art for %s\n", id3->path);
214 /* Write the size string, e.g. ".100x100". */
215 snprintf(size_string, sizeof(size_string), ".%dx%d",
216 data->albumart_max_width, data->albumart_max_height);
218 /* First we look for a bitmap of the right size */
219 if (search_albumart_files(id3, size_string, buf, buflen))
220 return true;
222 /* Then we look for generic bitmaps */
223 *size_string = 0;
224 return search_albumart_files(id3, size_string, buf, buflen);
227 /* Draw the album art bitmap from the given handle ID onto the given WPS.
228 Call with clear = true to clear the bitmap instead of drawing it. */
229 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
231 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
232 return;
234 struct wps_data *data = gwps->data;
236 #ifdef HAVE_REMOTE_LCD
237 /* No album art on RWPS */
238 if (data->remote_wps)
239 return;
240 #endif
242 struct bitmap *bmp;
243 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
244 return;
246 short x = data->albumart_x;
247 short y = data->albumart_y;
248 short width = bmp->width;
249 short height = bmp->height;
251 if (data->albumart_max_width > 0)
253 /* Crop if the bitmap is too wide */
254 width = MIN(bmp->width, data->albumart_max_width);
256 /* Align */
257 if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT)
258 x += data->albumart_max_width - width;
259 else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER)
260 x += (data->albumart_max_width - width) / 2;
263 if (data->albumart_max_height > 0)
265 /* Crop if the bitmap is too high */
266 height = MIN(bmp->height, data->albumart_max_height);
268 /* Align */
269 if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM)
270 y += data->albumart_max_height - height;
271 else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER)
272 y += (data->albumart_max_height - height) / 2;
275 if (!clear)
277 /* Draw the bitmap */
278 gwps->display->set_drawmode(DRMODE_FG);
279 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width,
280 x, y, width, height);
281 gwps->display->set_drawmode(DRMODE_SOLID);
283 else
285 /* Clear the bitmap */
286 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
287 gwps->display->fillrect(x, y, width, height);
288 gwps->display->set_drawmode(DRMODE_SOLID);