1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 #include "buffering.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
)
49 if (!buf
|| buf_size
<= 0 || !fullpath
)
52 /* if 'fullpath' is only a filename return immediately */
53 sep
= strrchr(fullpath
, '/');
57 return (char*)fullpath
;
60 len
= MIN(sep
- fullpath
+ 1, buf_size
- 1);
61 strncpy(buf
, fullpath
, len
);
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
[] = "*/:<>?\\|";
81 for (i
= 0; i
<= count
; i
++, path
++)
87 else if (strchr(invalid_chars
, *path
))
92 /* Look for the first matching album art bitmap in the following list:
93 * ./<trackname><size>.bmp
94 * ./<albumname><size>.bmp
96 * ../<albumname><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];
110 const char *trackname
;
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
);
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
);
147 /* look in the albumart subdir of .rockbox */
148 snprintf(path
, sizeof(path
),
149 ROCKBOX_DIR
"/albumart/%s-%s%s.bmp",
153 fix_path_part(path
, strlen(ROCKBOX_DIR
"/albumart/"), MAX_PATH
);
154 found
= file_exists(path
);
159 /* if it still doesn't exist,
160 * we continue to search in the parent directory */
162 path
[dirlen
- 1] = 0;
163 strip_filename(dir
, sizeof(dir
), path
);
164 dirlen
= strlen(dir
);
167 /* only try parent if there is one */
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
);
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
);
193 strncpy(buf
, path
, buflen
);
194 DEBUGF("Album art found: %s\n", path
);
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
)
207 struct wps_data
*data
= gui_wps
[0].data
;
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
))
222 /* Then we look for generic bitmaps */
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)
234 struct wps_data
*data
= gwps
->data
;
236 #ifdef HAVE_REMOTE_LCD
237 /* No album art on RWPS */
238 if (data
->remote_wps
)
243 if (bufgetdata(handle_id
, 0, (void *)&bmp
) <= 0)
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
);
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
);
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;
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
);
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
);