Compare commits

..

No commits in common. "26b6b1f902e69b852431c2e510adbfbb3dbe009b" and "3934d9b1f90b354aed7a072103ef1c58e581cd9d" have entirely different histories.

219
nncmpp.c
View File

@ -209,35 +209,6 @@ 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
@ -2248,13 +2219,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");
@ -2266,14 +2237,15 @@ 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 - DURATION_MAX_LEN, attrs); row_buffer_align (buffer, width - length_len, attrs);
int duration = -1; char *s = NULL;
mpd_read_time (compact_map_find (map, "duration"), &duration, NULL); unsigned long n;
mpd_read_time (compact_map_find (map, "time"), &duration, NULL); const char *time = compact_map_find (map, "time");
if (!time || !xstrtoul (&n, time, 10) || !(s = app_time_string (n)))
s = xstrdup ("?");
char *s = duration < 0 ? xstrdup ("-") : app_time_string (duration); char *right_aligned = xstrdup_printf ("%*s", length_len, s);
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);
@ -2406,6 +2378,17 @@ 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
@ -2413,46 +2396,31 @@ 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
int duration; ///< Duration or -1 if N/A or unknown const char *name; ///< Visible name
char *name; ///< Visible name const char *path; ///< MPD path
const char *path; ///< MPD path (follows the name)
}; };
static struct
{
struct tab super; ///< Parent class
struct str path; ///< Current path
struct library_level *above; ///< Upper levels
/// Current items
ARRAY (struct library_tab_item, items)
bool searching; ///< Search mode is active
}
g_library_tab;
static void static void
library_tab_add (int type, int duration, const char *name, const char *path) library_tab_add (int type, const char *name, const char *path)
{ {
// Slightly reduce memory overhead while retaining friendly access strv_append_owned (&g_library_tab.items,
size_t name_len = strlen (name), path_len = strlen (path); xstrdup_printf ("%c%s%c%s", type, name, 0, path));
char *combined = xmalloc (++name_len + ++path_len); }
ARRAY_RESERVE (g_library_tab.items, 1); static struct library_tab_item
g_library_tab.items[g_library_tab.items_len++] = (struct library_tab_item) library_tab_resolve (const char *raw)
{ {
.type = type, struct library_tab_item item;
.duration = duration, item.type = *raw++;
.name = memcpy (combined, name, name_len), item.name = raw;
.path = memcpy (combined + name_len, path, path_len), item.path = strchr (raw, '\0') + 1;
}; return item;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -2462,27 +2430,21 @@ 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 = &g_library_tab.items[item_index]; struct library_tab_item x =
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
@ -2490,38 +2452,31 @@ 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 (struct library_tab_item *a, struct library_tab_item *b) library_tab_compare (char **a, char **b)
{ {
if (a->type != b->type) struct library_tab_item xa = library_tab_resolve (*a);
return a->type - b->type; struct library_tab_item xb = library_tab_resolve (*b);
return app_casecmp ((uint8_t *) a->path, (uint8_t *) b->path); if (xa.type != xb.type)
return xa.type - xb.type;
return app_casecmp ((uint8_t *) xa.path, (uint8_t *) xb.path);
} }
static char * static char *
@ -2589,25 +2544,16 @@ 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)
{ {
library_tab_reset (); strv_reset (&g_library_tab.items);
char *parent = library_tab_parent (); char *parent = library_tab_parent ();
if (parent) if (parent)
{ {
library_tab_add (LIBRARY_ROOT, -1, "", ""); library_tab_add (LIBRARY_ROOT, "", "");
library_tab_add (LIBRARY_UP, -1, "", parent); library_tab_add (LIBRARY_UP, "", parent);
free (parent); free (parent);
} }
@ -2627,16 +2573,16 @@ library_tab_load_data (const struct strv *data)
} }
str_map_free (&map); str_map_free (&map);
struct library_tab_item *items = g_library_tab.items; struct strv *items = &g_library_tab.items;
size_t len = g_library_tab.super.item_count = g_library_tab.items_len; qsort (items->vector, items->len, sizeof *items->vector,
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) len) if (g_library_tab.super.item_selected >= (int) items->len)
app_move_selection (0); app_move_selection (0);
app_invalidate (); app_invalidate ();
@ -2724,8 +2670,9 @@ 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 = &g_library_tab.items[i]; struct library_tab_item x =
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE) library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
return true; return true;
} }
return false; return false;
@ -2743,7 +2690,9 @@ library_tab_on_action (enum action action)
if (range.from < 0) if (range.from < 0)
return false; return false;
struct library_tab_item *x = &g_library_tab.items[range.from]; struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[range.from]);
switch (action) switch (action)
{ {
case ACTION_CHOOSE: case ACTION_CHOOSE:
@ -2751,12 +2700,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;
@ -2805,9 +2754,10 @@ 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 = &g_library_tab.items[i]; struct library_tab_item x =
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE) library_tab_resolve (g_library_tab.items.vector[i]);
MPD_SIMPLE ("add", x->path); if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
MPD_SIMPLE ("add", x.path);
} }
tab->item_mark = -1; tab->item_mark = -1;
return true; return true;
@ -2823,9 +2773,10 @@ 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 = &g_library_tab.items[i]; struct library_tab_item x =
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE) library_tab_resolve (g_library_tab.items.vector[i]);
mpd_client_send_command (c, "add", x->path, NULL); if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
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);
@ -2845,7 +2796,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 is fine with zero initialisation g_library_tab.items = strv_make ();
struct tab *super = &g_library_tab.super; struct tab *super = &g_library_tab.super;
tab_init (super, "Library"); tab_init (super, "Library");
@ -3401,6 +3352,26 @@ 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)
{ {