Refactor the library tab, track duration
The `struct strv` was clunky, it's better to store items directly in the format we use for all processing. The additional memory cost is negligible.
This commit is contained in:
parent
791c000791
commit
0dc29a3e2d
141
nncmpp.c
141
nncmpp.c
|
@ -2406,17 +2406,6 @@ struct library_level
|
||||||
char path[]; ///< Path of the level
|
char path[]; ///< Path of the level
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct
|
|
||||||
{
|
|
||||||
struct tab super; ///< Parent class
|
|
||||||
struct str path; ///< Current path
|
|
||||||
struct strv items; ///< Current items (type, name, path)
|
|
||||||
struct library_level *above; ///< Upper levels
|
|
||||||
|
|
||||||
bool searching; ///< Search mode is active
|
|
||||||
}
|
|
||||||
g_library_tab;
|
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
// This list is also ordered by ASCII and important for sorting
|
// This list is also ordered by ASCII and important for sorting
|
||||||
|
@ -2430,25 +2419,39 @@ enum
|
||||||
struct library_tab_item
|
struct library_tab_item
|
||||||
{
|
{
|
||||||
int type; ///< Type of the item
|
int type; ///< Type of the item
|
||||||
const char *name; ///< Visible name
|
int duration; ///< Duration or -1 if N/A or unknown
|
||||||
const char *path; ///< MPD path
|
char *name; ///< Visible name
|
||||||
|
const char *path; ///< MPD path (follows the name)
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static struct
|
||||||
library_tab_add (int type, const char *name, const char *path)
|
|
||||||
{
|
{
|
||||||
strv_append_owned (&g_library_tab.items,
|
struct tab super; ///< Parent class
|
||||||
xstrdup_printf ("%c%s%c%s", type, name, 0, path));
|
struct str path; ///< Current path
|
||||||
}
|
struct library_level *above; ///< Upper levels
|
||||||
|
|
||||||
static struct library_tab_item
|
/// Current items
|
||||||
library_tab_resolve (const char *raw)
|
ARRAY (struct library_tab_item, items)
|
||||||
|
|
||||||
|
bool searching; ///< Search mode is active
|
||||||
|
}
|
||||||
|
g_library_tab;
|
||||||
|
|
||||||
|
static void
|
||||||
|
library_tab_add (int type, int duration, const char *name, const char *path)
|
||||||
{
|
{
|
||||||
struct library_tab_item item;
|
// Slightly reduce memory overhead while retaining friendly access
|
||||||
item.type = *raw++;
|
size_t name_len = strlen (name), path_len = strlen (path);
|
||||||
item.name = raw;
|
char *combined = xmalloc (++name_len + ++path_len);
|
||||||
item.path = strchr (raw, '\0') + 1;
|
|
||||||
return item;
|
ARRAY_RESERVE (g_library_tab.items, 1);
|
||||||
|
g_library_tab.items[g_library_tab.items_len++] = (struct library_tab_item)
|
||||||
|
{
|
||||||
|
.type = type,
|
||||||
|
.duration = duration,
|
||||||
|
.name = memcpy (combined, name, name_len),
|
||||||
|
.path = memcpy (combined + name_len, path, path_len),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
@ -2458,20 +2461,19 @@ library_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
|
||||||
int width)
|
int width)
|
||||||
{
|
{
|
||||||
(void) width;
|
(void) width;
|
||||||
hard_assert (item_index < g_library_tab.items.len);
|
hard_assert (item_index < g_library_tab.items_len);
|
||||||
|
|
||||||
struct library_tab_item x =
|
struct library_tab_item *x = &g_library_tab.items[item_index];
|
||||||
library_tab_resolve (g_library_tab.items.vector[item_index]);
|
|
||||||
const char *prefix, *name;
|
const char *prefix, *name;
|
||||||
switch (x.type)
|
switch (x->type)
|
||||||
{
|
{
|
||||||
case LIBRARY_ROOT: prefix = "/"; name = ""; break;
|
case LIBRARY_ROOT: prefix = "/"; name = ""; break;
|
||||||
case LIBRARY_UP: prefix = "/"; name = ".."; break;
|
case LIBRARY_UP: prefix = "/"; name = ".."; break;
|
||||||
case LIBRARY_DIR: prefix = "/"; name = x.name; break;
|
case LIBRARY_DIR: prefix = "/"; name = x->name; break;
|
||||||
case LIBRARY_FILE: prefix = " "; name = x.name; break;
|
case LIBRARY_FILE: prefix = " "; name = x->name; break;
|
||||||
default: hard_assert (!"invalid item type");
|
default: hard_assert (!"invalid item type");
|
||||||
}
|
}
|
||||||
chtype attrs = x.type != LIBRARY_FILE ? APP_ATTR (DIRECTORY) : 0;
|
chtype attrs = x->type != LIBRARY_FILE ? APP_ATTR (DIRECTORY) : 0;
|
||||||
row_buffer_append_args (buffer, prefix, attrs, name, attrs, NULL);
|
row_buffer_append_args (buffer, prefix, attrs, name, attrs, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2491,20 +2493,21 @@ library_tab_chunk (char type, const char *path, struct str_map *map)
|
||||||
char *name = (artist && title)
|
char *name = (artist && title)
|
||||||
? xstrdup_printf ("%s - %s", artist, title)
|
? xstrdup_printf ("%s - %s", artist, title)
|
||||||
: xstrdup (xbasename (path));
|
: xstrdup (xbasename (path));
|
||||||
library_tab_add (type, name, path);
|
|
||||||
|
int duration = -1;
|
||||||
|
mpd_read_time (str_map_find (map, "duration"), &duration, NULL);
|
||||||
|
mpd_read_time (str_map_find (map, "time"), &duration, NULL);
|
||||||
|
library_tab_add (type, duration, name, path);
|
||||||
free (name);
|
free (name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
library_tab_compare (char **a, char **b)
|
library_tab_compare (struct library_tab_item *a, struct library_tab_item *b)
|
||||||
{
|
{
|
||||||
struct library_tab_item xa = library_tab_resolve (*a);
|
if (a->type != b->type)
|
||||||
struct library_tab_item xb = library_tab_resolve (*b);
|
return a->type - b->type;
|
||||||
|
|
||||||
if (xa.type != xb.type)
|
return app_casecmp ((uint8_t *) a->path, (uint8_t *) b->path);
|
||||||
return xa.type - xb.type;
|
|
||||||
|
|
||||||
return app_casecmp ((uint8_t *) xa.path, (uint8_t *) xb.path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
|
@ -2572,16 +2575,25 @@ library_tab_change_level (const char *new_path)
|
||||||
g_library_tab.super.header = xstrdup_printf ("/%s", path->str);
|
g_library_tab.super.header = xstrdup_printf ("/%s", path->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
library_tab_reset (void)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < g_library_tab.items_len; i++)
|
||||||
|
free (g_library_tab.items[i].name);
|
||||||
|
free (g_library_tab.items);
|
||||||
|
ARRAY_INIT (g_library_tab.items);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
library_tab_load_data (const struct strv *data)
|
library_tab_load_data (const struct strv *data)
|
||||||
{
|
{
|
||||||
strv_reset (&g_library_tab.items);
|
library_tab_reset ();
|
||||||
|
|
||||||
char *parent = library_tab_parent ();
|
char *parent = library_tab_parent ();
|
||||||
if (parent)
|
if (parent)
|
||||||
{
|
{
|
||||||
library_tab_add (LIBRARY_ROOT, "", "");
|
library_tab_add (LIBRARY_ROOT, -1, "", "");
|
||||||
library_tab_add (LIBRARY_UP, "", parent);
|
library_tab_add (LIBRARY_UP, -1, "", parent);
|
||||||
free (parent);
|
free (parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2601,16 +2613,16 @@ library_tab_load_data (const struct strv *data)
|
||||||
}
|
}
|
||||||
str_map_free (&map);
|
str_map_free (&map);
|
||||||
|
|
||||||
struct strv *items = &g_library_tab.items;
|
struct library_tab_item *items = g_library_tab.items;
|
||||||
qsort (items->vector, items->len, sizeof *items->vector,
|
size_t len = g_library_tab.super.item_count = g_library_tab.items_len;
|
||||||
|
qsort (items, len, sizeof *items,
|
||||||
(int (*) (const void *, const void *)) library_tab_compare);
|
(int (*) (const void *, const void *)) library_tab_compare);
|
||||||
g_library_tab.super.item_count = items->len;
|
|
||||||
|
|
||||||
// XXX: this unmarks even if just the database updates
|
// XXX: this unmarks even if just the database updates
|
||||||
g_library_tab.super.item_mark = -1;
|
g_library_tab.super.item_mark = -1;
|
||||||
|
|
||||||
// Don't force the selection visible when there's no need to touch it
|
// Don't force the selection visible when there's no need to touch it
|
||||||
if (g_library_tab.super.item_selected >= (int) items->len)
|
if (g_library_tab.super.item_selected >= (int) len)
|
||||||
app_move_selection (0);
|
app_move_selection (0);
|
||||||
|
|
||||||
app_invalidate ();
|
app_invalidate ();
|
||||||
|
@ -2698,9 +2710,8 @@ library_tab_is_range_playable (struct tab_range range)
|
||||||
{
|
{
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
for (int i = range.from; i <= range.upto; i++)
|
||||||
{
|
{
|
||||||
struct library_tab_item x =
|
struct library_tab_item *x = &g_library_tab.items[i];
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -2718,9 +2729,7 @@ library_tab_on_action (enum action action)
|
||||||
if (range.from < 0)
|
if (range.from < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
struct library_tab_item x =
|
struct library_tab_item *x = &g_library_tab.items[range.from];
|
||||||
library_tab_resolve (g_library_tab.items.vector[range.from]);
|
|
||||||
|
|
||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case ACTION_CHOOSE:
|
case ACTION_CHOOSE:
|
||||||
|
@ -2728,12 +2737,12 @@ library_tab_on_action (enum action action)
|
||||||
if (range.from != range.upto)
|
if (range.from != range.upto)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
switch (x.type)
|
switch (x->type)
|
||||||
{
|
{
|
||||||
case LIBRARY_ROOT:
|
case LIBRARY_ROOT:
|
||||||
case LIBRARY_UP:
|
case LIBRARY_UP:
|
||||||
case LIBRARY_DIR: library_tab_reload (x.path); break;
|
case LIBRARY_DIR: library_tab_reload (x->path); break;
|
||||||
case LIBRARY_FILE: MPD_SIMPLE ("add", x.path); break;
|
case LIBRARY_FILE: MPD_SIMPLE ("add", x->path); break;
|
||||||
default: hard_assert (!"invalid item type");
|
default: hard_assert (!"invalid item type");
|
||||||
}
|
}
|
||||||
tab->item_mark = -1;
|
tab->item_mark = -1;
|
||||||
|
@ -2782,10 +2791,9 @@ library_tab_on_action (enum action action)
|
||||||
|
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
for (int i = range.from; i <= range.upto; i++)
|
||||||
{
|
{
|
||||||
struct library_tab_item x =
|
struct library_tab_item *x = &g_library_tab.items[i];
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
MPD_SIMPLE ("add", x->path);
|
||||||
MPD_SIMPLE ("add", x.path);
|
|
||||||
}
|
}
|
||||||
tab->item_mark = -1;
|
tab->item_mark = -1;
|
||||||
return true;
|
return true;
|
||||||
|
@ -2801,10 +2809,9 @@ library_tab_on_action (enum action action)
|
||||||
mpd_client_send_command (c, "clear", NULL);
|
mpd_client_send_command (c, "clear", NULL);
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
for (int i = range.from; i <= range.upto; i++)
|
||||||
{
|
{
|
||||||
struct library_tab_item x =
|
struct library_tab_item *x = &g_library_tab.items[i];
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
mpd_client_send_command (c, "add", x->path, NULL);
|
||||||
mpd_client_send_command (c, "add", x.path, NULL);
|
|
||||||
}
|
}
|
||||||
if (g.state == PLAYER_PLAYING)
|
if (g.state == PLAYER_PLAYING)
|
||||||
mpd_client_send_command (c, "play", NULL);
|
mpd_client_send_command (c, "play", NULL);
|
||||||
|
@ -2824,7 +2831,7 @@ static struct tab *
|
||||||
library_tab_init (void)
|
library_tab_init (void)
|
||||||
{
|
{
|
||||||
g_library_tab.path = str_make ();
|
g_library_tab.path = str_make ();
|
||||||
g_library_tab.items = strv_make ();
|
// g_library_tab.items is fine with zero initialisation
|
||||||
|
|
||||||
struct tab *super = &g_library_tab.super;
|
struct tab *super = &g_library_tab.super;
|
||||||
tab_init (super, "Library");
|
tab_init (super, "Library");
|
||||||
|
|
Loading…
Reference in New Issue