Compare commits

...

5 Commits

Author SHA1 Message Date
26b6b1f902
Show song duration in the library
Ideally we'd make columns configurable, which isn't trivial.

This brings the "Current" and "Library" tabs closer together.

Closes #2
2020-10-24 14:58:53 +02:00
8121046be6
Skip playlists in lsinfo responses
Instead of merging the fields into other items.
2020-10-24 14:58:48 +02:00
0dc29a3e2d
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.
2020-10-24 14:55:25 +02:00
791c000791
Use '-' instead of '?' for unknown duration
It is less distracting.

Also use mpd_read_time() and load "duration" as well.
This value isn't rounded to whole seconds, so we load
it before "time" as a fail-safe measure.
2020-10-24 14:54:17 +02:00
c0119027b1
Improve the MPD time parser
- reject negative values, which strtoul() happily accepts
 - deal with an arbitrary number of decimal digits
 - don't return milliseconds when we fail to parse seconds
2020-10-24 14:54:12 +02:00

219
nncmpp.c
View File

@ -209,6 +209,35 @@ mpd_parse_kv (char *line, char **value)
return key; return key;
} }
static void
mpd_read_time (const char *value, int *sec, int *optional_msec)
{
if (!value)
return;
char *end = NULL;
long n = strtol (value, &end, 10);
if (n < 0 || (*end && *end != '.'))
return;
int msec = 0;
if (*end == '.')
{
// In practice, MPD always uses three decimal digits
size_t digits = strspn (++end, "0123456789");
if (end[digits])
return;
if (digits--) msec += (*end++ - '0') * 100;
if (digits--) msec += (*end++ - '0') * 10;
if (digits--) msec += *end++ - '0';
}
*sec = MIN (INT_MAX, n);
if (optional_msec)
*optional_msec = msec;
}
// --- cURL async wrapper ------------------------------------------------------ // --- cURL async wrapper ------------------------------------------------------
// You are meant to subclass this structure, no user_data pointers needed // You are meant to subclass this structure, no user_data pointers needed
@ -2219,13 +2248,13 @@ app_process_termo_event (termo_key_t *event)
static struct tab g_current_tab; static struct tab g_current_tab;
#define DURATION_MAX_LEN (1 /*separator */ + 2 /* h */ + 3 /* m */+ 3 /* s */)
static void static void
current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
int width) int width)
{ {
// TODO: configurable output, maybe dynamically sized columns // TODO: configurable output, maybe dynamically sized columns
int length_len = 1 /*separator */ + 2 /* h */ + 3 /* m */+ 3 /* s */;
compact_map_t map = item_list_get (&g.playlist, item_index); compact_map_t map = item_list_get (&g.playlist, item_index);
const char *artist = compact_map_find (map, "artist"); const char *artist = compact_map_find (map, "artist");
const char *title = compact_map_find (map, "title"); const char *title = compact_map_find (map, "title");
@ -2237,15 +2266,14 @@ current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
else else
row_buffer_append (buffer, compact_map_find (map, "file"), attrs); row_buffer_append (buffer, compact_map_find (map, "file"), attrs);
row_buffer_align (buffer, width - length_len, attrs); row_buffer_align (buffer, width - DURATION_MAX_LEN, attrs);
char *s = NULL; int duration = -1;
unsigned long n; mpd_read_time (compact_map_find (map, "duration"), &duration, NULL);
const char *time = compact_map_find (map, "time"); mpd_read_time (compact_map_find (map, "time"), &duration, NULL);
if (!time || !xstrtoul (&n, time, 10) || !(s = app_time_string (n)))
s = xstrdup ("?");
char *right_aligned = xstrdup_printf ("%*s", length_len, s); char *s = duration < 0 ? xstrdup ("-") : app_time_string (duration);
char *right_aligned = xstrdup_printf ("%*s", DURATION_MAX_LEN, s);
row_buffer_append (buffer, right_aligned, attrs); row_buffer_append (buffer, right_aligned, attrs);
free (right_aligned); free (right_aligned);
free (s); free (s);
@ -2378,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
@ -2396,31 +2413,46 @@ enum
LIBRARY_ROOT = '/', ///< Root entry LIBRARY_ROOT = '/', ///< Root entry
LIBRARY_UP = '^', ///< Upper directory LIBRARY_UP = '^', ///< Upper directory
LIBRARY_DIR = 'd', ///< Directory LIBRARY_DIR = 'd', ///< Directory
LIBRARY_FILE = 'f' ///< File LIBRARY_FILE = 'f', ///< File
LIBRARY_PLAYLIST = 'p', ///< Playlist (unsupported)
}; };
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),
};
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -2430,21 +2462,27 @@ 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);
if (x->duration < 0)
return;
char *s = app_time_string (x->duration);
row_buffer_align (buffer, width - 2 /* gap */ - strlen (s), 0);
row_buffer_append_args (buffer, " " /* gap */, 0, s, 0, NULL);
free (s);
} }
static char static char
@ -2452,31 +2490,38 @@ library_tab_header_type (const char *key)
{ {
if (!strcasecmp_ascii (key, "file")) return LIBRARY_FILE; if (!strcasecmp_ascii (key, "file")) return LIBRARY_FILE;
if (!strcasecmp_ascii (key, "directory")) return LIBRARY_DIR; if (!strcasecmp_ascii (key, "directory")) return LIBRARY_DIR;
if (!strcasecmp_ascii (key, "playlist")) return LIBRARY_PLAYLIST;
return 0; return 0;
} }
static void static void
library_tab_chunk (char type, const char *path, struct str_map *map) library_tab_chunk (char type, const char *path, struct str_map *map)
{ {
// CUE files appear once as a directory and another time as a playlist,
// just skip them entirely
if (type == LIBRARY_PLAYLIST)
return;
const char *artist = str_map_find (map, "artist"); const char *artist = str_map_find (map, "artist");
const char *title = str_map_find (map, "title"); const char *title = str_map_find (map, "title");
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 *
@ -2544,16 +2589,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);
} }
@ -2573,16 +2627,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 ();
@ -2670,9 +2724,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;
@ -2690,9 +2743,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:
@ -2700,12 +2751,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;
@ -2754,10 +2805,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;
@ -2773,10 +2823,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);
@ -2796,7 +2845,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");
@ -3352,26 +3401,6 @@ debug_tab_init (void)
// --- MPD interface ----------------------------------------------------------- // --- MPD interface -----------------------------------------------------------
static void
mpd_read_time (const char *value, int *sec, int *optional_msec)
{
if (!value)
return;
char *end, *period = strchr (value, '.');
if (optional_msec && period)
{
unsigned long n = strtoul (period + 1, &end, 10);
if (*end)
return;
// XXX: this relies on three decimal places
*optional_msec = MIN (INT_MAX, n);
}
unsigned long n = strtoul (value, &end, 10);
if (end == period || !*end)
*sec = MIN (INT_MAX, n);
}
static void static void
mpd_update_playlist_time (void) mpd_update_playlist_time (void)
{ {