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 ****************************************************************************/
22 #include "string-extra.h"
26 #include "buffering.h"
32 /* Define LOGF_ENABLE to enable logf output in this file */
33 /*#define LOGF_ENABLE*/
36 #if defined(HAVE_JPEG) || defined(PLUGIN)
37 #define USE_JPEG_COVER
40 /* Strip filename from a full path
42 * buf - buffer to extract directory to.
43 * buf_size - size of buffer.
44 * fullpath - fullpath to extract from.
46 * Split the directory part of the given fullpath and store it in buf
47 * (including last '/').
48 * The function return parameter is a pointer to the filename
49 * inside the given fullpath.
51 static char* strip_filename(char* buf
, int buf_size
, const char* fullpath
)
56 if (!buf
|| buf_size
<= 0 || !fullpath
)
59 /* if 'fullpath' is only a filename return immediately */
60 sep
= strrchr(fullpath
, '/');
64 return (char*)fullpath
;
67 len
= MIN(sep
- fullpath
+ 1, buf_size
- 1);
68 strlcpy(buf
, fullpath
, len
+ 1);
72 /* Make sure part of path only contain chars valid for a FAT32 long name.
73 * Double quotes are replaced with single quotes, other unsupported chars
74 * are replaced with an underscore.
76 * path - path to modify.
77 * offset - where in path to start checking.
78 * count - number of chars to check.
80 static void fix_path_part(char* path
, int offset
, int count
)
82 static const char invalid_chars
[] = "*/:<>?\\|";
87 for (i
= 0; i
<= count
; i
++, path
++)
93 else if (strchr(invalid_chars
, *path
))
99 static const char * extensions
[] = { "jpeg", "jpg", "bmp" };
100 static int extension_lens
[] = { 4, 3, 3 };
101 /* Try checking for several file extensions, return true if a file is found and
102 * leaving the path modified to include the matching extension.
104 static bool try_exts(char *path
, int len
)
107 for (i
= 0; i
< 3; i
++)
109 if (extension_lens
[i
] + len
> MAX_PATH
)
111 strcpy(path
+ len
, extensions
[i
]);
112 if (file_exists(path
))
120 #define try_exts(path, len) file_exists(path)
123 /* Look for the first matching album art bitmap in the following list:
124 * ./<trackname><size>.{jpeg,jpg,bmp}
125 * ./<albumname><size>.{jpeg,jpg,bmp}
127 * ../<albumname><size>.{jpeg,jpg,bmp}
128 * ../cover<size>.{jpeg,jpg,bmp}
129 * ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
130 * <size> is the value of the size_string parameter, <trackname> and
131 * <albumname> are read from the ID3 metadata.
132 * If a matching bitmap is found, its filename is stored in buf.
133 * Return value is true if a bitmap was found, false otherwise.
135 * If the first symbol in size_string is a colon (e.g. ":100x100")
136 * then the colon is skipped ("100x100" will be used) and the track
137 * specific image (./<trackname><size>.bmp) is tried last instead of first.
139 bool search_albumart_files(const struct mp3entry
*id3
, const char *size_string
,
140 char *buf
, int buflen
)
142 char path
[MAX_PATH
+ 1];
143 char dir
[MAX_PATH
+ 1];
147 const char *trackname
;
156 trackname
= id3
->path
;
158 if (strcmp(trackname
, "No file!") == 0)
161 if (*size_string
== ':')
167 strip_filename(dir
, sizeof(dir
), trackname
);
168 dirlen
= strlen(dir
);
169 albumlen
= id3
->album
? strlen(id3
->album
) : 0;
171 for(pass
= 0; pass
< 2 - track_first
; pass
++)
173 if (track_first
|| pass
)
175 /* the first file we look for is one specific to the
177 strip_extension(path
, sizeof(path
) - strlen(size_string
) - 4,
179 strcat(path
, size_string
);
180 strcat(path
, "." EXT
);
181 #ifdef USE_JPEG_COVER
182 pathlen
= strlen(path
);
184 found
= try_exts(path
, pathlen
);
188 if (!found
&& albumlen
> 0)
190 /* if it doesn't exist,
191 * we look for a file specific to the track's album name */
192 pathlen
= snprintf(path
, sizeof(path
),
193 "%s%s%s." EXT
, dir
, id3
->album
, size_string
);
194 fix_path_part(path
, dirlen
, albumlen
);
195 found
= try_exts(path
, pathlen
);
200 /* if it still doesn't exist, we look for a generic file */
201 pathlen
= snprintf(path
, sizeof(path
),
202 "%scover%s." EXT
, dir
, size_string
);
203 found
= try_exts(path
, pathlen
);
206 #ifdef USE_JPEG_COVER
207 if (!found
&& !*size_string
)
209 snprintf (path
, sizeof(path
), "%sfolder.jpg", dir
);
210 found
= file_exists(path
);
214 artist
= id3
->albumartist
!= NULL
? id3
->albumartist
: id3
->artist
;
216 if (!found
&& artist
&& id3
->album
)
218 /* look in the albumart subdir of .rockbox */
219 pathlen
= snprintf(path
, sizeof(path
),
220 ROCKBOX_DIR
"/albumart/%s-%s%s." EXT
,
224 fix_path_part(path
, strlen(ROCKBOX_DIR
"/albumart/"), MAX_PATH
);
225 found
= try_exts(path
, pathlen
);
230 /* if it still doesn't exist,
231 * we continue to search in the parent directory */
233 path
[dirlen
- 1] = 0;
234 strip_filename(dir
, sizeof(dir
), path
);
235 dirlen
= strlen(dir
);
238 /* only try parent if there is one */
241 if (!found
&& albumlen
> 0)
243 /* we look in the parent directory
244 * for a file specific to the track's album name */
245 pathlen
= snprintf(path
, sizeof(path
),
246 "%s%s%s." EXT
, dir
, id3
->album
, size_string
);
247 fix_path_part(path
, dirlen
, albumlen
);
248 found
= try_exts(path
, pathlen
);
253 /* if it still doesn't exist, we look in the parent directory
254 * for a generic file */
255 pathlen
= snprintf(path
, sizeof(path
),
256 "%scover%s." EXT
, dir
, size_string
);
257 found
= try_exts(path
, pathlen
);
267 strlcpy(buf
, path
, buflen
);
268 logf("Album art found: %s", path
);
273 /* Look for albumart bitmap in the same dir as the track and in its parent dir.
274 * Stores the found filename in the buf parameter.
275 * Returns true if a bitmap was found, false otherwise */
276 bool find_albumart(const struct mp3entry
*id3
, char *buf
, int buflen
,
283 logf("Looking for album art for %s", id3
->path
);
285 /* Write the size string, e.g. ".100x100". */
286 snprintf(size_string
, sizeof(size_string
), ".%dx%d",
287 dim
->width
, dim
->height
);
289 /* First we look for a bitmap of the right size */
290 if (search_albumart_files(id3
, size_string
, buf
, buflen
))
293 /* Then we look for generic bitmaps */
295 return search_albumart_files(id3
, size_string
, buf
, buflen
);
298 /* Draw the album art bitmap from the given handle ID onto the given WPS.
299 Call with clear = true to clear the bitmap instead of drawing it. */
300 void draw_album_art(struct gui_wps
*gwps
, int handle_id
, bool clear
)
302 if (!gwps
|| !gwps
->data
|| !gwps
->display
|| handle_id
< 0)
305 struct wps_data
*data
= gwps
->data
;
306 struct skin_albumart
*aa
= data
->albumart
;
312 if (bufgetdata(handle_id
, 0, (void *)&bmp
) <= 0)
317 short width
= bmp
->width
;
318 short height
= bmp
->height
;
322 /* Crop if the bitmap is too wide */
323 width
= MIN(bmp
->width
, aa
->width
);
326 if (aa
->xalign
& WPS_ALBUMART_ALIGN_RIGHT
)
327 x
+= aa
->width
- width
;
328 else if (aa
->xalign
& WPS_ALBUMART_ALIGN_CENTER
)
329 x
+= (aa
->width
- width
) / 2;
334 /* Crop if the bitmap is too high */
335 height
= MIN(bmp
->height
, aa
->height
);
338 if (aa
->yalign
& WPS_ALBUMART_ALIGN_BOTTOM
)
339 y
+= aa
->height
- height
;
340 else if (aa
->yalign
& WPS_ALBUMART_ALIGN_CENTER
)
341 y
+= (aa
->height
- height
) / 2;
346 /* Draw the bitmap */
347 gwps
->display
->bitmap_part((fb_data
*)bmp
->data
, 0, 0,
348 STRIDE(gwps
->display
->screen_type
,
349 bmp
->width
, bmp
->height
),
350 x
, y
, width
, height
);
351 #ifdef HAVE_LCD_INVERT
352 if (global_settings
.invert
) {
353 gwps
->display
->set_drawmode(DRMODE_COMPLEMENT
);
354 gwps
->display
->fillrect(x
, y
, width
, height
);
355 gwps
->display
->set_drawmode(DRMODE_SOLID
);
361 /* Clear the bitmap */
362 gwps
->display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
363 gwps
->display
->fillrect(x
, y
, width
, height
);
364 gwps
->display
->set_drawmode(DRMODE_SOLID
);