2007-11-11 12:29:37 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Nicolas Pennequin
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-11-11 12:29:37 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "sprintf.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "albumart.h"
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2007-11-11 12:29:37 +00:00
|
|
|
#include "buffering.h"
|
|
|
|
#include "dircache.h"
|
2007-11-21 21:28:27 +00:00
|
|
|
#include "misc.h"
|
2008-01-19 18:05:06 +00:00
|
|
|
#include "settings.h"
|
2009-08-03 15:06:30 +00:00
|
|
|
#include "wps.h"
|
2007-11-11 12:29:37 +00:00
|
|
|
|
2009-06-21 22:00:11 +00:00
|
|
|
/* Define LOGF_ENABLE to enable logf output in this file */
|
|
|
|
/*#define LOGF_ENABLE*/
|
|
|
|
#include "logf.h"
|
|
|
|
|
2009-05-16 07:34:36 +00:00
|
|
|
#if defined(HAVE_JPEG) || defined(PLUGIN)
|
|
|
|
#define USE_JPEG_COVER
|
|
|
|
#endif
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
/* Strip filename from a full path
|
|
|
|
*
|
|
|
|
* buf - buffer to extract directory to.
|
|
|
|
* buf_size - size of buffer.
|
|
|
|
* fullpath - fullpath to extract from.
|
|
|
|
*
|
|
|
|
* Split the directory part of the given fullpath and store it in buf
|
|
|
|
* (including last '/').
|
|
|
|
* The function return parameter is a pointer to the filename
|
|
|
|
* inside the given fullpath.
|
|
|
|
*/
|
|
|
|
static char* strip_filename(char* buf, int buf_size, const char* fullpath)
|
|
|
|
{
|
|
|
|
char* sep;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!buf || buf_size <= 0 || !fullpath)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* if 'fullpath' is only a filename return immediately */
|
|
|
|
sep = strrchr(fullpath, '/');
|
|
|
|
if (sep == NULL)
|
|
|
|
{
|
|
|
|
buf[0] = 0;
|
|
|
|
return (char*)fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = MIN(sep - fullpath + 1, buf_size - 1);
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(buf, fullpath, len + 1);
|
2007-11-11 12:29:37 +00:00
|
|
|
return (sep + 1);
|
|
|
|
}
|
|
|
|
|
2007-11-15 20:22:03 +00:00
|
|
|
/* Make sure part of path only contain chars valid for a FAT32 long name.
|
|
|
|
* Double quotes are replaced with single quotes, other unsupported chars
|
|
|
|
* are replaced with an underscore.
|
|
|
|
*
|
|
|
|
* path - path to modify.
|
|
|
|
* offset - where in path to start checking.
|
|
|
|
* count - number of chars to check.
|
|
|
|
*/
|
|
|
|
static void fix_path_part(char* path, int offset, int count)
|
|
|
|
{
|
|
|
|
static const char invalid_chars[] = "*/:<>?\\|";
|
|
|
|
int i;
|
|
|
|
|
|
|
|
path += offset;
|
|
|
|
|
|
|
|
for (i = 0; i <= count; i++, path++)
|
|
|
|
{
|
2008-01-19 18:05:06 +00:00
|
|
|
if (*path == 0)
|
|
|
|
return;
|
2007-11-15 20:22:03 +00:00
|
|
|
if (*path == '"')
|
|
|
|
*path = '\'';
|
|
|
|
else if (strchr(invalid_chars, *path))
|
|
|
|
*path = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 07:34:36 +00:00
|
|
|
#ifdef USE_JPEG_COVER
|
2009-05-16 06:59:29 +00:00
|
|
|
static const char * extensions[] = { "jpeg", "jpg", "bmp" };
|
|
|
|
static int extension_lens[] = { 4, 3, 3 };
|
2009-05-01 23:31:43 +00:00
|
|
|
/* Try checking for several file extensions, return true if a file is found and
|
|
|
|
* leaving the path modified to include the matching extension.
|
|
|
|
*/
|
|
|
|
static bool try_exts(char *path, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if (extension_lens[i] + len > MAX_PATH)
|
|
|
|
continue;
|
|
|
|
strcpy(path + len, extensions[i]);
|
|
|
|
if (file_exists(path))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-16 08:20:47 +00:00
|
|
|
#define EXT
|
|
|
|
#else
|
|
|
|
#define EXT "bmp"
|
|
|
|
#define try_exts(path, len) file_exists(path)
|
2009-05-01 23:31:43 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-11 12:29:37 +00:00
|
|
|
/* Look for the first matching album art bitmap in the following list:
|
2009-05-28 20:05:31 +00:00
|
|
|
* ./<trackname><size>.{jpeg,jpg,bmp}
|
|
|
|
* ./<albumname><size>.{jpeg,jpg,bmp}
|
2007-11-11 12:29:37 +00:00
|
|
|
* ./cover<size>.bmp
|
2009-05-28 20:05:31 +00:00
|
|
|
* ../<albumname><size>.{jpeg,jpg,bmp}
|
|
|
|
* ../cover<size>.{jpeg,jpg,bmp}
|
|
|
|
* ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
|
2007-11-11 12:29:37 +00:00
|
|
|
* <size> is the value of the size_string parameter, <trackname> and
|
|
|
|
* <albumname> are read from the ID3 metadata.
|
|
|
|
* If a matching bitmap is found, its filename is stored in buf.
|
|
|
|
* Return value is true if a bitmap was found, false otherwise.
|
2009-05-28 20:05:31 +00:00
|
|
|
*
|
|
|
|
* If the first symbol in size_string is a colon (e.g. ":100x100")
|
|
|
|
* then the colon is skipped ("100x100" will be used) and the track
|
2009-05-29 10:31:56 +00:00
|
|
|
* specific image (./<trackname><size>.bmp) is tried last instead of first.
|
2007-11-11 12:29:37 +00:00
|
|
|
*/
|
2007-12-09 18:48:02 +00:00
|
|
|
bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
|
|
|
|
char *buf, int buflen)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
char path[MAX_PATH + 1];
|
|
|
|
char dir[MAX_PATH + 1];
|
|
|
|
bool found = false;
|
2009-05-29 10:31:56 +00:00
|
|
|
int track_first = 1;
|
|
|
|
int pass;
|
2007-11-11 12:29:37 +00:00
|
|
|
const char *trackname;
|
2008-04-19 15:21:09 +00:00
|
|
|
const char *artist;
|
2007-11-15 20:22:03 +00:00
|
|
|
int dirlen;
|
|
|
|
int albumlen;
|
2009-05-01 23:31:43 +00:00
|
|
|
int pathlen;
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
if (!id3 || !buf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
trackname = id3->path;
|
2008-02-05 11:56:21 +00:00
|
|
|
|
|
|
|
if (strcmp(trackname, "No file!") == 0)
|
|
|
|
return false;
|
|
|
|
|
2009-05-28 04:34:07 +00:00
|
|
|
if (*size_string == ':')
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
2009-05-29 10:31:56 +00:00
|
|
|
size_string++;
|
|
|
|
track_first = 0;
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 10:31:56 +00:00
|
|
|
strip_filename(dir, sizeof(dir), trackname);
|
|
|
|
dirlen = strlen(dir);
|
|
|
|
albumlen = id3->album ? strlen(id3->album) : 0;
|
2008-01-19 18:05:06 +00:00
|
|
|
|
2009-05-29 10:58:23 +00:00
|
|
|
for(pass = 0; pass < 2 - track_first; pass++)
|
2009-05-01 23:31:43 +00:00
|
|
|
{
|
2009-05-29 10:31:56 +00:00
|
|
|
if (track_first || pass)
|
|
|
|
{
|
|
|
|
/* the first file we look for is one specific to the
|
|
|
|
current track */
|
|
|
|
strip_extension(path, sizeof(path) - strlen(size_string) - 4,
|
|
|
|
trackname);
|
|
|
|
strcat(path, size_string);
|
|
|
|
strcat(path, "." EXT);
|
|
|
|
#ifdef USE_JPEG_COVER
|
|
|
|
pathlen = strlen(path);
|
2009-05-01 23:31:43 +00:00
|
|
|
#endif
|
2009-05-29 10:31:56 +00:00
|
|
|
found = try_exts(path, pathlen);
|
|
|
|
}
|
|
|
|
if (pass)
|
|
|
|
break;
|
2007-11-15 20:22:03 +00:00
|
|
|
if (!found && albumlen > 0)
|
|
|
|
{
|
2009-05-29 10:31:56 +00:00
|
|
|
/* if it doesn't exist,
|
|
|
|
* we look for a file specific to the track's album name */
|
2009-05-01 23:31:43 +00:00
|
|
|
pathlen = snprintf(path, sizeof(path),
|
2009-05-29 10:31:56 +00:00
|
|
|
"%s%s%s." EXT, dir, id3->album, size_string);
|
2009-05-01 23:31:43 +00:00
|
|
|
fix_path_part(path, dirlen, albumlen);
|
|
|
|
found = try_exts(path, pathlen);
|
2007-11-15 20:22:03 +00:00
|
|
|
}
|
2009-05-29 10:31:56 +00:00
|
|
|
|
2007-11-15 20:22:03 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
2009-05-29 10:31:56 +00:00
|
|
|
/* if it still doesn't exist, we look for a generic file */
|
2009-05-01 23:31:43 +00:00
|
|
|
pathlen = snprintf(path, sizeof(path),
|
2009-05-29 10:31:56 +00:00
|
|
|
"%scover%s." EXT, dir, size_string);
|
2009-05-01 23:31:43 +00:00
|
|
|
found = try_exts(path, pathlen);
|
2007-11-15 20:22:03 +00:00
|
|
|
}
|
2009-05-29 10:31:56 +00:00
|
|
|
|
|
|
|
#ifdef USE_JPEG_COVER
|
|
|
|
if (!found && !*size_string)
|
|
|
|
{
|
|
|
|
snprintf (path, sizeof(path), "%sfolder.jpg", dir);
|
|
|
|
found = file_exists(path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
|
|
|
|
|
|
|
|
if (!found && artist && id3->album)
|
|
|
|
{
|
|
|
|
/* look in the albumart subdir of .rockbox */
|
|
|
|
pathlen = snprintf(path, sizeof(path),
|
|
|
|
ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
|
|
|
|
artist,
|
|
|
|
id3->album,
|
|
|
|
size_string);
|
|
|
|
fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
|
|
|
|
found = try_exts(path, pathlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* if it still doesn't exist,
|
|
|
|
* we continue to search in the parent directory */
|
|
|
|
strcpy(path, dir);
|
|
|
|
path[dirlen - 1] = 0;
|
|
|
|
strip_filename(dir, sizeof(dir), path);
|
|
|
|
dirlen = strlen(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only try parent if there is one */
|
|
|
|
if (dirlen > 0)
|
|
|
|
{
|
|
|
|
if (!found && albumlen > 0)
|
|
|
|
{
|
|
|
|
/* we look in the parent directory
|
|
|
|
* for a file specific to the track's album name */
|
|
|
|
pathlen = snprintf(path, sizeof(path),
|
|
|
|
"%s%s%s." EXT, dir, id3->album, size_string);
|
|
|
|
fix_path_part(path, dirlen, albumlen);
|
|
|
|
found = try_exts(path, pathlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* if it still doesn't exist, we look in the parent directory
|
|
|
|
* for a generic file */
|
|
|
|
pathlen = snprintf(path, sizeof(path),
|
|
|
|
"%scover%s." EXT, dir, size_string);
|
|
|
|
found = try_exts(path, pathlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return false;
|
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(buf, path, buflen);
|
2009-06-22 18:40:49 +00:00
|
|
|
logf("Album art found: %s", path);
|
2007-11-11 12:29:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#ifndef PLUGIN
|
2007-11-11 12:29:37 +00:00
|
|
|
/* Look for albumart bitmap in the same dir as the track and in its parent dir.
|
|
|
|
* Stores the found filename in the buf parameter.
|
|
|
|
* Returns true if a bitmap was found, false otherwise */
|
2009-10-16 19:14:41 +00:00
|
|
|
bool find_albumart(const struct mp3entry *id3, char *buf, int buflen,
|
|
|
|
struct dim *dim)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
if (!id3 || !buf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char size_string[9];
|
2009-06-22 18:40:49 +00:00
|
|
|
logf("Looking for album art for %s", id3->path);
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
/* Write the size string, e.g. ".100x100". */
|
|
|
|
snprintf(size_string, sizeof(size_string), ".%dx%d",
|
2009-10-16 19:14:41 +00:00
|
|
|
dim->width, dim->height);
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
/* First we look for a bitmap of the right size */
|
2007-12-09 18:48:02 +00:00
|
|
|
if (search_albumart_files(id3, size_string, buf, buflen))
|
2007-11-11 12:29:37 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Then we look for generic bitmaps */
|
|
|
|
*size_string = 0;
|
2007-12-09 18:48:02 +00:00
|
|
|
return search_albumart_files(id3, size_string, buf, buflen);
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-12 01:31:42 +00:00
|
|
|
/* Draw the album art bitmap from the given handle ID onto the given WPS.
|
|
|
|
Call with clear = true to clear the bitmap instead of drawing it. */
|
|
|
|
void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct wps_data *data = gwps->data;
|
2009-09-07 02:36:56 +00:00
|
|
|
struct skin_albumart *aa = data->albumart;
|
2007-11-11 12:29:37 +00:00
|
|
|
|
2009-09-07 02:36:56 +00:00
|
|
|
if (!aa)
|
|
|
|
return;
|
|
|
|
|
2007-11-11 12:29:37 +00:00
|
|
|
struct bitmap *bmp;
|
2007-11-12 14:26:47 +00:00
|
|
|
if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
|
|
|
|
return;
|
2007-11-11 12:29:37 +00:00
|
|
|
|
2009-09-25 18:36:28 +00:00
|
|
|
short x = aa->x;
|
|
|
|
short y = aa->y;
|
2007-11-11 12:29:37 +00:00
|
|
|
short width = bmp->width;
|
|
|
|
short height = bmp->height;
|
|
|
|
|
2009-09-25 18:36:28 +00:00
|
|
|
if (aa->width > 0)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
/* Crop if the bitmap is too wide */
|
2009-09-25 18:36:28 +00:00
|
|
|
width = MIN(bmp->width, aa->width);
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
/* Align */
|
2009-09-25 18:36:28 +00:00
|
|
|
if (aa->xalign & WPS_ALBUMART_ALIGN_RIGHT)
|
|
|
|
x += aa->width - width;
|
|
|
|
else if (aa->xalign & WPS_ALBUMART_ALIGN_CENTER)
|
|
|
|
x += (aa->width - width) / 2;
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 18:36:28 +00:00
|
|
|
if (aa->height > 0)
|
2007-11-11 12:29:37 +00:00
|
|
|
{
|
|
|
|
/* Crop if the bitmap is too high */
|
2009-09-25 18:36:28 +00:00
|
|
|
height = MIN(bmp->height, aa->height);
|
2007-11-11 12:29:37 +00:00
|
|
|
|
|
|
|
/* Align */
|
2009-09-25 18:36:28 +00:00
|
|
|
if (aa->yalign & WPS_ALBUMART_ALIGN_BOTTOM)
|
|
|
|
y += aa->height - height;
|
|
|
|
else if (aa->yalign & WPS_ALBUMART_ALIGN_CENTER)
|
|
|
|
y += (aa->height - height) / 2;
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-12 01:31:42 +00:00
|
|
|
if (!clear)
|
|
|
|
{
|
|
|
|
/* Draw the bitmap */
|
2009-09-04 00:46:24 +00:00
|
|
|
gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0,
|
|
|
|
STRIDE(gwps->display->screen_type,
|
|
|
|
bmp->width, bmp->height),
|
2007-11-12 01:31:42 +00:00
|
|
|
x, y, width, height);
|
2009-03-19 17:43:53 +00:00
|
|
|
#ifdef HAVE_LCD_INVERT
|
|
|
|
if (global_settings.invert) {
|
|
|
|
gwps->display->set_drawmode(DRMODE_COMPLEMENT);
|
|
|
|
gwps->display->fillrect(x, y, width, height);
|
|
|
|
gwps->display->set_drawmode(DRMODE_SOLID);
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-12 01:31:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Clear the bitmap */
|
|
|
|
gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
|
|
|
gwps->display->fillrect(x, y, width, height);
|
|
|
|
gwps->display->set_drawmode(DRMODE_SOLID);
|
|
|
|
}
|
2007-11-11 12:29:37 +00:00
|
|
|
}
|
2008-12-09 23:07:59 +00:00
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#endif /* PLUGIN */
|