1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
28 #include "buffering.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
)
51 if (!buf
|| buf_size
<= 0 || !fullpath
)
54 /* if 'fullpath' is only a filename return immediately */
55 sep
= strrchr(fullpath
, '/');
59 return (char*)fullpath
;
62 len
= MIN(sep
- fullpath
+ 1, buf_size
- 1);
63 strncpy(buf
, fullpath
, len
);
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
[] = "*/:<>?\\|";
83 for (i
= 0; i
<= count
; i
++, path
++)
89 else if (strchr(invalid_chars
, *path
))
94 /* Look for the first matching album art bitmap in the following list:
95 * ./<trackname><size>.bmp
96 * ./<albumname><size>.bmp
98 * ../<albumname><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];
112 const char *trackname
;
120 trackname
= id3
->path
;
122 if (strcmp(trackname
, "No file!") == 0)
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
);
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",
162 fix_path_part(path
, strlen(ROCKBOX_DIR
"/albumart/"), MAX_PATH
);
163 found
= file_exists(path
);
168 /* if it still doesn't exist,
169 * we continue to search in the parent directory */
171 path
[dirlen
- 1] = 0;
172 strip_filename(dir
, sizeof(dir
), path
);
173 dirlen
= strlen(dir
);
176 /* only try parent if there is one */
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
);
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
);
202 strncpy(buf
, path
, buflen
);
203 DEBUGF("Album art found: %s\n", path
);
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
)
216 struct wps_data
*data
= gui_wps
[0].data
;
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
))
231 /* Then we look for generic bitmaps */
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)
243 struct wps_data
*data
= gwps
->data
;
245 #ifdef HAVE_REMOTE_LCD
246 /* No album art on RWPS */
247 if (data
->remote_wps
)
252 if (bufgetdata(handle_id
, 0, (void *)&bmp
) <= 0)
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
);
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
);
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;
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
);
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
);