Introduce names for header and row color in the tables so that they are the same...
[kugel-rb.git] / apps / recorder / albumart.c
blob4507923144bd7f0427d66bc4c7bbdc14d4567bfc
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 "buffering.h"
28 #include "dircache.h"
29 #include "misc.h"
30 #include "settings.h"
31 #include "wps.h"
33 /* Define LOGF_ENABLE to enable logf output in this file */
34 /*#define LOGF_ENABLE*/
35 #include "logf.h"
37 #if defined(HAVE_JPEG) || defined(PLUGIN)
38 #define USE_JPEG_COVER
39 #endif
41 /* Strip filename from a full path
43 * buf - buffer to extract directory to.
44 * buf_size - size of buffer.
45 * fullpath - fullpath to extract from.
47 * Split the directory part of the given fullpath and store it in buf
48 * (including last '/').
49 * The function return parameter is a pointer to the filename
50 * inside the given fullpath.
52 static char* strip_filename(char* buf, int buf_size, const char* fullpath)
54 char* sep;
55 int len;
57 if (!buf || buf_size <= 0 || !fullpath)
58 return NULL;
60 /* if 'fullpath' is only a filename return immediately */
61 sep = strrchr(fullpath, '/');
62 if (sep == NULL)
64 buf[0] = 0;
65 return (char*)fullpath;
68 len = MIN(sep - fullpath + 1, buf_size - 1);
69 strlcpy(buf, fullpath, len + 1);
70 return (sep + 1);
73 /* Make sure part of path only contain chars valid for a FAT32 long name.
74 * Double quotes are replaced with single quotes, other unsupported chars
75 * are replaced with an underscore.
77 * path - path to modify.
78 * offset - where in path to start checking.
79 * count - number of chars to check.
81 static void fix_path_part(char* path, int offset, int count)
83 static const char invalid_chars[] = "*/:<>?\\|";
84 int i;
86 path += offset;
88 for (i = 0; i <= count; i++, path++)
90 if (*path == 0)
91 return;
92 if (*path == '"')
93 *path = '\'';
94 else if (strchr(invalid_chars, *path))
95 *path = '_';
99 #ifdef USE_JPEG_COVER
100 static const char * extensions[] = { "jpeg", "jpg", "bmp" };
101 static int extension_lens[] = { 4, 3, 3 };
102 /* Try checking for several file extensions, return true if a file is found and
103 * leaving the path modified to include the matching extension.
105 static bool try_exts(char *path, int len)
107 int i;
108 for (i = 0; i < 3; i++)
110 if (extension_lens[i] + len > MAX_PATH)
111 continue;
112 strcpy(path + len, extensions[i]);
113 if (file_exists(path))
114 return true;
116 return false;
118 #define EXT
119 #else
120 #define EXT "bmp"
121 #define try_exts(path, len) file_exists(path)
122 #endif
124 /* Look for the first matching album art bitmap in the following list:
125 * ./<trackname><size>.{jpeg,jpg,bmp}
126 * ./<albumname><size>.{jpeg,jpg,bmp}
127 * ./cover<size>.bmp
128 * ../<albumname><size>.{jpeg,jpg,bmp}
129 * ../cover<size>.{jpeg,jpg,bmp}
130 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
131 * <size> is the value of the size_string parameter, <trackname> and
132 * <albumname> are read from the ID3 metadata.
133 * If a matching bitmap is found, its filename is stored in buf.
134 * Return value is true if a bitmap was found, false otherwise.
136 * If the first symbol in size_string is a colon (e.g. ":100x100")
137 * then the colon is skipped ("100x100" will be used) and the track
138 * specific image (./<trackname><size>.bmp) is tried last instead of first.
140 bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
141 char *buf, int buflen)
143 char path[MAX_PATH + 1];
144 char dir[MAX_PATH + 1];
145 bool found = false;
146 int track_first = 1;
147 int pass;
148 const char *trackname;
149 const char *artist;
150 int dirlen;
151 int albumlen;
152 int pathlen;
154 if (!id3 || !buf)
155 return false;
157 trackname = id3->path;
159 if (strcmp(trackname, "No file!") == 0)
160 return false;
162 if (*size_string == ':')
164 size_string++;
165 track_first = 0;
168 strip_filename(dir, sizeof(dir), trackname);
169 dirlen = strlen(dir);
170 albumlen = id3->album ? strlen(id3->album) : 0;
172 for(pass = 0; pass < 2 - track_first; pass++)
174 if (track_first || pass)
176 /* the first file we look for is one specific to the
177 current track */
178 strip_extension(path, sizeof(path) - strlen(size_string) - 4,
179 trackname);
180 strcat(path, size_string);
181 strcat(path, "." EXT);
182 #ifdef USE_JPEG_COVER
183 pathlen = strlen(path);
184 #endif
185 found = try_exts(path, pathlen);
187 if (pass)
188 break;
189 if (!found && albumlen > 0)
191 /* if it doesn't exist,
192 * we look for a file specific to the track's album name */
193 pathlen = snprintf(path, sizeof(path),
194 "%s%s%s." EXT, dir, id3->album, size_string);
195 fix_path_part(path, dirlen, albumlen);
196 found = try_exts(path, pathlen);
199 if (!found)
201 /* if it still doesn't exist, we look for a generic file */
202 pathlen = snprintf(path, sizeof(path),
203 "%scover%s." EXT, dir, size_string);
204 found = try_exts(path, pathlen);
207 #ifdef USE_JPEG_COVER
208 if (!found && !*size_string)
210 snprintf (path, sizeof(path), "%sfolder.jpg", dir);
211 found = file_exists(path);
213 #endif
215 artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
217 if (!found && artist && id3->album)
219 /* look in the albumart subdir of .rockbox */
220 pathlen = snprintf(path, sizeof(path),
221 ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
222 artist,
223 id3->album,
224 size_string);
225 fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
226 found = try_exts(path, pathlen);
229 if (!found)
231 /* if it still doesn't exist,
232 * we continue to search in the parent directory */
233 strcpy(path, dir);
234 path[dirlen - 1] = 0;
235 strip_filename(dir, sizeof(dir), path);
236 dirlen = strlen(dir);
239 /* only try parent if there is one */
240 if (dirlen > 0)
242 if (!found && albumlen > 0)
244 /* we look in the parent directory
245 * for a file specific to the track's album name */
246 pathlen = snprintf(path, sizeof(path),
247 "%s%s%s." EXT, dir, id3->album, size_string);
248 fix_path_part(path, dirlen, albumlen);
249 found = try_exts(path, pathlen);
252 if (!found)
254 /* if it still doesn't exist, we look in the parent directory
255 * for a generic file */
256 pathlen = snprintf(path, sizeof(path),
257 "%scover%s." EXT, dir, size_string);
258 found = try_exts(path, pathlen);
261 if (found)
262 break;
265 if (!found)
266 return false;
268 strlcpy(buf, path, buflen);
269 logf("Album art found: %s", path);
270 return true;
273 #ifndef PLUGIN
274 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
275 * Stores the found filename in the buf parameter.
276 * Returns true if a bitmap was found, false otherwise */
277 bool find_albumart(const struct mp3entry *id3, char *buf, int buflen)
279 if (!id3 || !buf)
280 return false;
282 char size_string[9];
283 int width = 0, height = 0;
285 if (!wps_uses_albumart(&width, &height))
286 return false;
288 logf("Looking for album art for %s", id3->path);
290 /* Write the size string, e.g. ".100x100". */
291 snprintf(size_string, sizeof(size_string), ".%dx%d",
292 width, height);
294 /* First we look for a bitmap of the right size */
295 if (search_albumart_files(id3, size_string, buf, buflen))
296 return true;
298 /* Then we look for generic bitmaps */
299 *size_string = 0;
300 return search_albumart_files(id3, size_string, buf, buflen);
303 /* Draw the album art bitmap from the given handle ID onto the given WPS.
304 Call with clear = true to clear the bitmap instead of drawing it. */
305 void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
307 if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
308 return;
310 struct wps_data *data = gwps->data;
312 #ifdef HAVE_REMOTE_LCD
313 /* No album art on RWPS */
314 if (data->remote_wps)
315 return;
316 #endif
318 struct bitmap *bmp;
319 if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
320 return;
322 short x = data->albumart_x;
323 short y = data->albumart_y;
324 short width = bmp->width;
325 short height = bmp->height;
327 if (data->albumart_max_width > 0)
329 /* Crop if the bitmap is too wide */
330 width = MIN(bmp->width, data->albumart_max_width);
332 /* Align */
333 if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT)
334 x += data->albumart_max_width - width;
335 else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER)
336 x += (data->albumart_max_width - width) / 2;
339 if (data->albumart_max_height > 0)
341 /* Crop if the bitmap is too high */
342 height = MIN(bmp->height, data->albumart_max_height);
344 /* Align */
345 if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM)
346 y += data->albumart_max_height - height;
347 else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER)
348 y += (data->albumart_max_height - height) / 2;
351 if (!clear)
353 /* Draw the bitmap */
354 gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width,
355 x, y, width, height);
356 #ifdef HAVE_LCD_INVERT
357 if (global_settings.invert) {
358 gwps->display->set_drawmode(DRMODE_COMPLEMENT);
359 gwps->display->fillrect(x, y, width, height);
360 gwps->display->set_drawmode(DRMODE_SOLID);
362 #endif
364 else
366 /* Clear the bitmap */
367 gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
368 gwps->display->fillrect(x, y, width, height);
369 gwps->display->set_drawmode(DRMODE_SOLID);
373 void get_albumart_size(struct bitmap *bmp)
375 /* FIXME: What should we do with albumart on remote? */
376 int width, height;
378 if (!wps_uses_albumart(&width, &height))
380 width = 0; height = 0;
383 bmp->width = width;
384 bmp->height = height;
386 #endif /* PLUGIN */