Skip to content

Commit

Permalink
FF.CFG: New option sort-priority=folders|files|none
Browse files Browse the repository at this point in the history
Default sort priority is folders first, then files.

Also fix bug with folders >1000 items.
  • Loading branch information
keirf committed May 23, 2019
1 parent 756333a commit 892d5e0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
14 changes: 11 additions & 3 deletions examples/FF.CFG
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,19 @@ autoselect-file-secs = 2
autoselect-folder-secs = 2

# Sorting of folder entries in native navigation mode.
# always: Always sort folder entries. Large folders may be truncated.
# never: Never sort folder entries, instead presenting them in FAT order.
# small: Only sort folders which are small enough to sort in full.
# always: Always sort folder entries. Large folders may be truncated.
# never: Never sort folder entries, instead presenting them in FAT order.
# small: Only sort folders which are small enough to sort in full.
# Values: always | never | small
folder-sort = always

# Priority of files vs subfolders when sorting folder entries:
# folders: Folders listed before files
# files: Files listed before folders
# none: Files and folders are not differentiated
# Values: folders | files | none
sort-priority = folders

# Navigation mode for selecting images/slots
# native: Navigate through all valid images/dirs
# indexed: Navigate through DSKA0000, DSKA0001, ...
Expand Down
4 changes: 4 additions & 0 deletions inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ struct __packed ff_cfg {
uint8_t folder_sort;
#define MOTOR_ignore 0xff
uint8_t motor_delay; /* / 10ms */
#define SORTPRI_folders 0
#define SORTPRI_files 1
#define SORTPRI_none 2
uint8_t sort_priority;
};

extern struct ff_cfg ff_cfg;
Expand Down
3 changes: 3 additions & 0 deletions inc/intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct exception_frame {
#define _STR(x) #x
#define STR(x) _STR(x)

/* Force a compilation error if condition is true */
#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })

#define __aligned(x) __attribute__((aligned(x)))
#define __packed __attribute((packed))
#define always_inline __inline__ __attribute__((always_inline))
Expand Down
4 changes: 3 additions & 1 deletion scripts/mk_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main(argv):
out_f.write("/* Autogenerated by " + argv[0] + " */\n")
for line in in_f:
match = re.match("[ \t]*([A-Za-z0-9-]+)[ \t]*=[ \t]*"
"([A-Za-z0-9-]+|\".*\")", line)
"([A-Za-z0-9-,]+|\".*\")", line)
if match:
opt = match.group(1)
val = match.group(2)
Expand Down Expand Up @@ -59,6 +59,8 @@ def main(argv):
val = "NAVMODE_" + val
elif opt == "folder-sort":
val = "SORT_" + val
elif opt == "sort-priority":
val = "SORTPRI_" + val
elif opt == "motor-delay":
if val == 'ignore':
val = "MOTOR_" + val
Expand Down
2 changes: 2 additions & 0 deletions src/flash_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ void flash_ff_cfg_read(void)
union cfg_slot *slot = cfg_slot_find();
bool_t found = slot_is_valid(slot);

BUILD_BUG_ON(sizeof(*slot) != sizeof(slot->words));

ff_cfg = dfl_ff_cfg;
printk("Config: ");
if (found) {
Expand Down
49 changes: 35 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ static struct {
} *fs;

struct native_dirent {
uint32_t dir_sect, dir_ptr;
uint32_t dir_sect;
uint16_t dir_off;
uint8_t attr;
char name[0];
};

Expand Down Expand Up @@ -617,14 +619,23 @@ static int native_dir_cmp(const void *a, const void *b)
{
const struct native_dirent *da = a;
const struct native_dirent *db = b;
if ((da->attr ^ db->attr) & AM_DIR) {
switch (ff_cfg.sort_priority) {
case SORTPRI_folders:
return (da->attr & AM_DIR) ? -1 : 1;
case SORTPRI_files:
return (da->attr & AM_DIR) ? 1 : -1;
}
}
return strcmp_lower(da->name, db->name);
}

static unsigned int native_read_dir(void)
{
struct native_dirent **p_ent, **p_ents = arena_alloc(0);
struct native_dirent *ent = (struct native_dirent *)(p_ents + 1000);
char *end = arena_alloc(0) + arena_avail();
struct native_dirent **p_ent;
struct native_dirent *ent = arena_alloc(0);
char *start = arena_alloc(0);
char *end = start + arena_avail();
int nr;

if (ff_cfg.folder_sort == SORT_never)
Expand All @@ -633,13 +644,16 @@ static unsigned int native_read_dir(void)
volume_cache_destroy();

F_opendir(&fs->dp, "");
p_ent = p_ents;
while ((end - (char *)ent) > (FF_MAX_LFN + 1 + sizeof(*ent))) {
p_ent = (struct native_dirent **)end;
while (((char *)p_ent - (char *)ent)
> (FF_MAX_LFN + 1 + sizeof(*ent) + sizeof(ent))) {
if (!native_dir_next())
goto complete;
*p_ent++ = ent;
*--p_ent = ent;
ASSERT((unsigned int)(fs->fp.dir_ptr - fatfs.win) < 512u);
ent->dir_sect = fs->fp.dir_sect;
ent->dir_ptr = (uint32_t)fs->fp.dir_ptr;
ent->dir_off = fs->fp.dir_ptr - fatfs.win;
ent->attr = fs->fp.fattrib;
strcpy(ent->name, fs->fp.fname);
ent = (struct native_dirent *)(
((uint32_t)ent + sizeof(*ent) + strlen(ent->name) + 1 + 3) & ~3);
Expand All @@ -648,19 +662,19 @@ static unsigned int native_read_dir(void)
if (ff_cfg.folder_sort == SORT_always)
goto complete;

volume_cache_init(p_ents, end);
volume_cache_init(start, end);
cfg.sorted = NULL;
return 0;

complete:
nr = p_ent - p_ents;
nr = (struct native_dirent **)end - p_ent;

qsort_p(p_ents, nr, native_dir_cmp);
qsort_p(p_ent, nr, native_dir_cmp);

F_closedir(&fs->dp);

volume_cache_init(ent, end);
cfg.sorted = p_ents;
volume_cache_init(ent, p_ent);
cfg.sorted = p_ent;
return nr;
}

Expand Down Expand Up @@ -888,6 +902,13 @@ static void read_ff_cfg(void)
: SORT_always;
break;

case FFCFG_sort_priority:
ff_cfg.sort_priority =
!strcmp(opts.arg, "none") ? SORTPRI_none
: !strcmp(opts.arg, "files") ? SORTPRI_files
: SORTPRI_folders;
break;

case FFCFG_nav_mode:
ff_cfg.nav_mode =
!strcmp(opts.arg, "native") ? NAVMODE_native
Expand Down Expand Up @@ -1376,7 +1397,7 @@ static void native_update(uint8_t slot_mode)
snprintf(fs->fp.fname, sizeof(fs->fp.fname), ent->name);
fs->file.obj.fs = &fatfs;
fs->file.dir_sect = ent->dir_sect;
fs->file.dir_ptr = (BYTE *)ent->dir_ptr;
fs->file.dir_ptr = fatfs.win + ent->dir_off;
flashfloppy_fill_fileinfo(&fs->file);
fs->fp.fattrib = fs->file.obj.attr;
if (fs->file.obj.attr & AM_DIR)
Expand Down

0 comments on commit 892d5e0

Please sign in to comment.