1
0
Fork 0
forked from len0rd/rockbox

plugins: extract redundant functionality from stats/properties

Also:

- Enables cpu boosting for Stats
(*much* faster on some players)

- Displays file size in Stats

Change-Id: I888ba054e1f8c2985fbf8dca36e11e6ef130e7ca
This commit is contained in:
Christian Soffke 2024-07-25 20:49:20 +02:00
parent 6d19214eeb
commit 545271c4de
5 changed files with 252 additions and 337 deletions

View file

@ -40,6 +40,13 @@ struct multiple_tracks_id3 {
static struct multiple_tracks_id3 mul_id3;
static const int32_t units[] =
{
LANG_BYTE,
LANG_KIBIBYTE,
LANG_MEBIBYTE,
LANG_GIBIBYTE
};
/* Calculate modified FNV hash of string
* has good avalanche behaviour and uniform distribution
@ -130,9 +137,7 @@ void collect_id3(struct mp3entry *id3, bool is_first_track)
mul_id3.filesize += id3->filesize;
}
/* (!) Note scale factor applied to returned metadata:
* - Unit for filesize will be KiB instead of Bytes
* - Unit for length will be s instead of ms
/* (!) Note unit conversion below
*
* Use result only as input for browse_id3,
* with the track_ct parameter set to > 1.
@ -159,8 +164,8 @@ void finalize_id3(struct mp3entry *id3)
id3->track_string = NULL;
id3->year_string = NULL;
id3->year = mul_id3.year;
mul_id3.length /= 1000;
mul_id3.filesize >>= 10;
mul_id3.length /= 1000; /* convert from ms to s */
mul_id3.filesize >>= 10; /* convert from B to KiB */
id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length;
id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize;
id3->frequency = mul_id3.frequency;
@ -172,3 +177,176 @@ void finalize_id3(struct mp3entry *id3)
id3->track_level = 0;
id3->album_level = 0;
}
unsigned long human_size(unsigned long long byte_count, int32_t *unit_lang_id)
{
const size_t n = sizeof(units)/sizeof(units[0]);
unsigned int i;
/* margin set at 10K boundary: 10239 B +1 => 10 KB */
for(i = 0; i < n-1 && byte_count >= 10*1024; i++)
byte_count >>= 10; /* div by 1024 */
*unit_lang_id = units[i];
return (unsigned long)byte_count;
}
/* missing filetype attribute for images */
static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
/* and videos */
static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
static void prn(const char *str, int y)
{
rb->lcd_puts(0, y, str);
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_puts(0, y, str);
#endif
}
void display_dir_stats(struct dir_stats *stats)
{
char buf[32];
int32_t lang_size_unit;
unsigned long display_size = human_size(stats->byte_count, &lang_size_unit);
rb->lcd_clear_display();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_clear_display();
#endif
rb->snprintf(buf, sizeof(buf), "Files: %d (%lu %s)", stats->file_count,
display_size, rb->str(lang_size_unit));
prn(buf, 0);
rb->snprintf(buf, sizeof(buf), "Audio: %d", stats->audio_file_count);
prn(buf, 1);
if (stats->count_all)
{
rb->snprintf(buf, sizeof(buf), "Playlists: %d", stats->m3u_file_count);
prn(buf, 2);
rb->snprintf(buf, sizeof(buf), "Images: %d", stats->img_file_count);
prn(buf, 3);
rb->snprintf(buf, sizeof(buf), "Videos: %d", stats->vid_file_count);
prn(buf, 4);
rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count);
prn(buf, 5);
rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d",
stats->max_files_in_dir);
prn(buf, 6);
}
else
{
rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count);
prn(buf, 2);
}
rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_update();
#endif
}
/* Recursively scans directories in search of files
* and informs the user of the progress.
*/
bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*))
{
bool result = true;
unsigned int files_in_dir = 0;
static unsigned int id3_count;
static unsigned long last_displayed, last_get_action;
struct dirent* entry;
int dirlen = rb->strlen(stats->dirname);
DIR* dir = rb->opendir(stats->dirname);
if (!dir)
{
rb->splashf(HZ*2, "open error: %s", stats->dirname);
return false;
}
else if (!stats->dirname[1]) /* root dir */
stats->dirname[0] = dirlen = 0;
/* walk through the directory content */
while(result && (0 != (entry = rb->readdir(dir))))
{
struct dirinfo info = rb->dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY)
{
if (!rb->strcmp((char *)entry->d_name, ".") ||
!rb->strcmp((char *)entry->d_name, ".."))
continue; /* skip these */
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
if (!id3_cb)
{
stats->dir_count++; /* new directory */
if (*rb->current_tick - last_displayed > (HZ/2))
{
if (last_displayed)
display_dir_stats(stats);
last_displayed = *(rb->current_tick);
}
}
result = collect_dir_stats(stats, id3_cb); /* recursion */
}
else if (!id3_cb)
{
char *ptr;
stats->file_count++; /* new file */
files_in_dir++;
stats->byte_count += info.size;
int attr = rb->filetype_get_attr(entry->d_name);
if (attr == FILE_ATTR_AUDIO)
stats->audio_file_count++;
else if (attr == FILE_ATTR_M3U)
stats->m3u_file_count++;
/* image or video file attributes have to be compared manually */
else if (stats->count_all &&
(ptr = rb->strrchr(entry->d_name,'.')))
{
unsigned int i;
ptr++;
for(i = 0; i < ARRAYLEN(image_exts); i++)
{
if(!rb->strcasecmp(ptr, image_exts[i]))
{
stats->img_file_count++;
break;
}
}
if (i >= ARRAYLEN(image_exts)) {
for(i = 0; i < ARRAYLEN(video_exts); i++) {
if(!rb->strcasecmp(ptr, video_exts[i])) {
stats->vid_file_count++;
break;
}
}
}
}
}
else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
{
rb->splash_progress(id3_count++, stats->audio_file_count,
"%s (%s)",
rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT));
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
id3_cb(stats->dirname); /* allow metadata to be collected */
}
if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8))
{
if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
{
stats->canceled = true;
result = false;
}
last_get_action = *(rb->current_tick);
}
rb->yield();
}
rb->closedir(dir);
if (stats->max_files_in_dir < files_in_dir)
stats->max_files_in_dir = files_in_dir;
return result;
}

View file

@ -21,7 +21,39 @@
#ifndef MUL_ID3_H
#define MUL_ID3_H
struct dir_stats {
char dirname[MAX_PATH];
unsigned int dir_count;
unsigned int file_count;
unsigned int audio_file_count;
unsigned int m3u_file_count;
unsigned int img_file_count;
unsigned int vid_file_count;
unsigned int max_files_in_dir;
unsigned long long byte_count;
bool count_all;
bool canceled;
};
/* create mp3entry that contains matching metadata from multiple tracks */
void collect_id3(struct mp3entry *id3, bool is_first_track);
void finalize_id3(struct mp3entry *id3);
/* Traverse directory, collecting stats/track metadata.
*
* 1) If id3_cb is null, dir_properties calculates all dir stats, including the
* audio file count.
*
* 2) If id3_cb points to a function, dir_properties will call it for every audio
* file encountered, to allow the file's metadata to be collected. The displayed
* progress bar's maximum value is set to the audio file count.
* Stats are assumed to have already been generated by a preceding run.
*
* If the count_all parameter is set to false, images and videos are not counted,
* nor is the playlist, image, video or max file in dir count displayed.
*/
bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*));
void display_dir_stats(struct dir_stats *stats);
unsigned long human_size(unsigned long long byte_count, int32_t *unit_lang_id);
#endif /* MUL_ID3_H */

View file

@ -25,15 +25,6 @@
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif
struct dir_stats {
char dirname[MAX_PATH];
unsigned int dir_count;
unsigned int file_count;
unsigned int audio_file_count;
unsigned long long byte_count;
bool canceled;
};
enum props_types {
PROPS_FILE = 0,
PROPS_PLAYLIST,
@ -42,7 +33,7 @@ enum props_types {
PROPS_DIR
};
static int props_type = PROPS_FILE;
static int props_type;
static struct mp3entry id3;
static int mul_id3_count;
@ -57,8 +48,8 @@ static char str_audio_filecount[64];
static char str_date[64];
static char str_time[64];
static unsigned long nsize;
static int32_t size_unit;
static unsigned long display_size;
static int32_t lang_size_unit;
static struct tm tm;
#define NUM_FILE_PROPERTIES 5
@ -86,26 +77,6 @@ static const unsigned char* const props_dir[] =
ID2P(LANG_MENU_SHOW_ID3_INFO), str_audio_filecount,
};
static const int32_t units[] =
{
LANG_BYTE,
LANG_KIBIBYTE,
LANG_MEBIBYTE,
LANG_GIBIBYTE
};
static unsigned int human_size_log(unsigned long long size)
{
const size_t n = sizeof(units)/sizeof(units[0]);
unsigned int i;
/* margin set at 10K boundary: 10239 B +1 => 10 KB */
for(i=0; i < n-1 && size >= 10*1024; i++)
size >>= 10; /* div by 1024 */
return i;
}
static bool file_properties(const char* selected_file)
{
bool found = false;
@ -118,12 +89,9 @@ static bool file_properties(const char* selected_file)
struct dirinfo info = rb->dir_get_info(dir, entry);
if(!rb->strcmp(entry->d_name, str_filename))
{
unsigned int log;
log = human_size_log((unsigned long)info.size);
nsize = ((unsigned long)info.size) >> (log*10);
size_unit = units[log];
display_size = human_size(info.size, &lang_size_unit);
rb->snprintf(str_size, sizeof str_size, "%lu %s",
nsize, rb->str(size_unit));
display_size, rb->str(lang_size_unit));
rb->gmtime_r(&info.mtime, &tm);
rb->snprintf(str_date, sizeof str_date, "%04d/%02d/%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
@ -141,99 +109,10 @@ static bool file_properties(const char* selected_file)
return found;
}
/* Recursively scans directories in search of files
* and informs the user of the progress.
*/
static bool _dir_properties(struct dir_stats *stats, bool (*id3_cb)(const char*))
{
bool result = true;
static unsigned long last_lcd_update, last_get_action;
struct dirent* entry;
int dirlen = rb->strlen(stats->dirname);
DIR* dir = rb->opendir(stats->dirname);
if (!dir)
{
rb->splashf(HZ*2, "open error: %s", stats->dirname);
return false;
}
/* walk through the directory content */
while(result && (0 != (entry = rb->readdir(dir))))
{
struct dirinfo info = rb->dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY)
{
if (!rb->strcmp((char *)entry->d_name, ".") ||
!rb->strcmp((char *)entry->d_name, ".."))
continue; /* skip these */
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
if (!id3_cb)
{
stats->dir_count++; /* new directory */
if (*rb->current_tick - last_lcd_update > (HZ/2))
{
unsigned int log;
last_lcd_update = *(rb->current_tick);
rb->lcd_clear_display();
rb->lcd_putsf(0, 0, "Directories: %d", stats->dir_count);
rb->lcd_putsf(0, 1, "Files: %d (Audio: %d)",
stats->file_count, stats->audio_file_count);
log = human_size_log(stats->byte_count);
rb->lcd_putsf(0, 2, "Size: %lu %s",
(unsigned long)(stats->byte_count >> (10*log)),
rb->str(units[log]));
rb->lcd_update();
}
}
result = _dir_properties(stats, id3_cb); /* recursion */
}
else if (!id3_cb)
{
stats->file_count++; /* new file */
stats->byte_count += info.size;
if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
stats->audio_file_count++;
}
else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
{
rb->splash_progress(mul_id3_count, stats->audio_file_count, "%s (%s)",
rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT));
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
id3_cb(stats->dirname); /* allow metadata to be collected */
}
if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8))
{
if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
{
stats->canceled = true;
result = false;
}
last_get_action = *(rb->current_tick);
}
rb->yield();
}
rb->closedir(dir);
return result;
}
/* 1) If id3_cb is null, dir_properties calculates all dir stats, including the
* audio file count.
*
* 2) If id3_cb points to a function, dir_properties will call it for every audio
* file encountered, to allow the file's metadata to be collected. The displayed
* progress bar's maximum value is set to the audio file count.
* Stats are assumed to have already been generated by a preceding run.
*/
static bool dir_properties(const char* selected_file, struct dir_stats *stats,
bool (*id3_cb)(const char*))
{
unsigned int log;
bool success;
rb->strlcpy(stats->dirname, selected_file, sizeof(stats->dirname));
@ -243,8 +122,8 @@ static bool dir_properties(const char* selected_file, struct dir_stats *stats,
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(true);
#endif
success = _dir_properties(stats, id3_cb);
success = collect_dir_stats(stats, id3_cb);
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(false);
#endif
@ -259,10 +138,9 @@ static bool dir_properties(const char* selected_file, struct dir_stats *stats,
rb->snprintf(str_filecount, sizeof str_filecount, "%d", stats->file_count);
rb->snprintf(str_audio_filecount, sizeof str_filecount, "%d",
stats->audio_file_count);
log = human_size_log(stats->byte_count);
nsize = (long) (stats->byte_count >> (log*10));
size_unit = units[log];
rb->snprintf(str_size, sizeof str_size, "%ld %s", nsize, rb->str(size_unit));
display_size = human_size(stats->byte_count, &lang_size_unit);
rb->snprintf(str_size, sizeof str_size, "%lu %s", display_size,
rb->str(lang_size_unit));
}
return true;
}
@ -320,8 +198,8 @@ static int speak_property_selection(int selected_item, void *data)
rb->talk_file_or_spell(str_dirname, str_filename, NULL, true);
break;
case LANG_PROPERTIES_SIZE:
rb->talk_number(nsize, true);
rb->talk_id(size_unit, true);
rb->talk_number(display_size, true);
rb->talk_id(lang_size_unit, true);
break;
case LANG_PROPERTIES_DATE:
rb->talk_date(&tm, true);
@ -399,10 +277,8 @@ static int browse_file_or_dir(struct dir_stats *stats)
static bool determine_file_or_dir(void)
{
DIR* dir;
struct dirent* entry;
dir = rb->opendir(str_dirname);
DIR* dir = rb->opendir(str_dirname);
if (dir)
{
while(0 != (entry = rb->readdir(dir)))
@ -482,7 +358,6 @@ enum plugin_status plugin_start(const void* parameter)
{
int ret;
static struct dir_stats stats;
const char *file = parameter;
if(!parameter)
return PLUGIN_ERROR;
@ -490,12 +365,10 @@ enum plugin_status plugin_start(const void* parameter)
#ifdef HAVE_TOUCHSCREEN
rb->touchscreen_set_mode(rb->global_settings->touch_mode);
#endif
if (file[0] == '/') /* single file or folder selected */
{
const char* file_name = rb->strrchr(file, '/') + 1;
int dirlen = (file_name - file);
const int dirlen = (file_name - file);
rb->strlcpy(str_dirname, file, dirlen + 1);
rb->snprintf(str_filename, sizeof str_filename, "%s", file + dirlen);

View file

@ -19,184 +19,27 @@
*
****************************************************************************/
#include "plugin.h"
#include "lib/pluginlib_actions.h"
#include "lib/mul_id3.h" /* collect_dir_stats & display_dir_stats */
static int files, dirs, audiofiles, m3ufiles, imagefiles, videofiles, largestdir;
static int lasttick;
static bool cancel;
/* we use PLA */
#define STATS_STOP PLA_EXIT
#if (CONFIG_KEYPAD == IPOD_1G2G_PAD) \
|| (CONFIG_KEYPAD == IPOD_3G_PAD) \
|| (CONFIG_KEYPAD == IPOD_4G_PAD)
#define STATS_STOP2 PLA_UP
#else
#define STATS_STOP2 PLA_CANCEL
#endif
/* this set the context to use with PLA */
static const struct button_mapping *plugin_contexts[] = { pla_main_ctx };
/* we don't have yet a filetype attribute for image files */
static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
/* neither for video ones */
static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
static void prn(const char *str, int y)
{
rb->lcd_puts(0,y,str);
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_puts(0,y,str);
#endif
}
static void update_screen(void)
{
char buf[32];
rb->lcd_clear_display();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_clear_display();
#endif
rb->snprintf(buf, sizeof(buf), "Total Files: %d", files);
prn(buf,0);
rb->snprintf(buf, sizeof(buf), "Audio: %d", audiofiles);
prn(buf,1);
rb->snprintf(buf, sizeof(buf), "Playlists: %d", m3ufiles);
prn(buf,2);
rb->snprintf(buf, sizeof(buf), "Images: %d", imagefiles);
prn(buf,3);
rb->snprintf(buf, sizeof(buf), "Videos: %d", videofiles);
prn(buf,4);
rb->snprintf(buf, sizeof(buf), "Directories: %d", dirs);
prn(buf,5);
rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
prn(buf,6);
rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_update();
#endif
}
static void traversedir(char* location, char* name)
{
int button;
struct dirent *entry;
DIR* dir;
char fullpath[MAX_PATH];
int files_in_dir = 0;
rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
dir = rb->opendir(fullpath);
if (dir) {
entry = rb->readdir(dir);
while (entry) {
if (cancel)
break;
/* Skip .. and . */
if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
{
struct dirinfo info = rb->dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY) {
traversedir(fullpath, entry->d_name);
dirs++;
}
else {
files_in_dir++; files++;
/* get the filetype from the filename */
int attr = rb->filetype_get_attr(entry->d_name);
switch (attr & FILE_ATTR_MASK)
{
case FILE_ATTR_AUDIO:
audiofiles++;
break;
case FILE_ATTR_M3U:
m3ufiles++;
break;
default:
{
/* use hardcoded filetype_exts to count
* image and video files until we get
* new attributes added to filetypes.h */
char *ptr = rb->strrchr(entry->d_name,'.');
if(ptr) {
unsigned i;
ptr++;
for(i=0;i<ARRAYLEN(image_exts);i++) {
if(!rb->strcasecmp(ptr,image_exts[i])) {
imagefiles++; break;
}
}
if (i >= ARRAYLEN(image_exts)) {
/* not found above - try video files */
for(i=0;i<ARRAYLEN(video_exts);i++) {
if(!rb->strcasecmp(ptr,video_exts[i])) {
videofiles++; break;
}
}
}
}
} /* default: */
} /* switch */
}
}
if (*rb->current_tick - lasttick > (HZ/2)) {
update_screen();
lasttick = *rb->current_tick;
button = pluginlib_getaction(TIMEOUT_NOBLOCK, plugin_contexts,
ARRAYLEN(plugin_contexts));
if (button == STATS_STOP || button == STATS_STOP2) {
cancel = true;
break;
}
}
entry = rb->readdir(dir);
}
rb->closedir(dir);
}
if (largestdir < files_in_dir)
largestdir = files_in_dir;
}
/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
int button;
{
(void)parameter;
files = 0;
dirs = 0;
audiofiles = 0;
m3ufiles = 0;
imagefiles = 0;
videofiles = 0;
largestdir = 0;
cancel = false;
rb->splash(HZ, "Counting...");
update_screen();
lasttick = *rb->current_tick;
traversedir("", "");
if (cancel) {
rb->splash(HZ, "Aborted");
int button, success;
static struct dir_stats stats;
stats.dirname[0] = '/';
stats.count_all = true;
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(true);
#endif
display_dir_stats(&stats);
success = collect_dir_stats(&stats, NULL);
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(false);
#endif
if (!success)
return PLUGIN_OK;
}
update_screen();
display_dir_stats(&stats);
#ifdef HAVE_BACKLIGHT
#ifdef HAVE_REMOTE_LCD
rb->remote_backlight_on();
@ -204,21 +47,15 @@ enum plugin_status plugin_start(const void* parameter)
rb->backlight_on();
#endif
rb->splash(HZ, "Done");
update_screen();
display_dir_stats(&stats);
while (1) {
button = pluginlib_getaction(TIMEOUT_BLOCK, plugin_contexts,
ARRAYLEN(plugin_contexts));
switch (button) {
case STATS_STOP:
case STATS_STOP2:
switch (button = rb->get_action(CONTEXT_STD, TIMEOUT_BLOCK))
{
case ACTION_STD_CANCEL:
return PLUGIN_OK;
break;
default:
if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED;
}
break;
}
}
return PLUGIN_OK;

View file

@ -5,9 +5,4 @@ The stats plugin counts the directories and files
(the total number as well as the number
of audio, playlist, image and video files)
on your \dap{}.
Press
\nopt{IPOD_4G_PAD,IPOD_3G_PAD}{\PluginCancel{} or \PluginExit{}}
\opt{IPOD_4G_PAD,IPOD_3G_PAD}{\ButtonMenu}
to abort counting and
exit the plugin. Press it again to quit after counting has
finished.
Press \ActionStdCancel{} to exit the plugin.