Compare commits

..

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

233
nncmpp.c
View File

@ -209,35 +209,6 @@ mpd_parse_kv (char *line, char **value)
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 ------------------------------------------------------
// 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;
#define DURATION_MAX_LEN (1 /*separator */ + 2 /* h */ + 3 /* m */+ 3 /* s */)
static void
current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
int width)
{
// 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);
const char *artist = compact_map_find (map, "artist");
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
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;
mpd_read_time (compact_map_find (map, "duration"), &duration, NULL);
mpd_read_time (compact_map_find (map, "time"), &duration, NULL);
char *s = NULL;
unsigned long n;
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", DURATION_MAX_LEN, s);
char *right_aligned = xstrdup_printf ("%*s", length_len, s);
row_buffer_append (buffer, right_aligned, attrs);
free (right_aligned);
free (s);
@ -2406,53 +2378,49 @@ struct library_level
char path[]; ///< Path of the level
};
enum
{
// This list is also ordered by ASCII and important for sorting
LIBRARY_ROOT = '/', ///< Root entry
LIBRARY_UP = '^', ///< Upper directory
LIBRARY_DIR = 'd', ///< Directory
LIBRARY_FILE = 'f', ///< File
LIBRARY_PLAYLIST = 'p', ///< Playlist (unsupported)
};
struct library_tab_item
{
int type; ///< Type of the item
int duration; ///< Duration or -1 if N/A or unknown
char *name; ///< Visible name
const char *path; ///< MPD path (follows the name)
};
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
/// Current items
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)
enum
{
// Slightly reduce memory overhead while retaining friendly access
size_t name_len = strlen (name), path_len = strlen (path);
char *combined = xmalloc (++name_len + ++path_len);
// This list is also ordered by ASCII and important for sorting
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),
};
LIBRARY_ROOT = '/', ///< Root entry
LIBRARY_UP = '^', ///< Upper directory
LIBRARY_DIR = 'd', ///< Directory
LIBRARY_FILE = 'f' ///< File
};
struct library_tab_item
{
int type; ///< Type of the item
const char *name; ///< Visible name
const char *path; ///< MPD path
};
static void
library_tab_add (int type, const char *name, const char *path)
{
strv_append_owned (&g_library_tab.items,
xstrdup_printf ("%c%s%c%s", type, name, 0, path));
}
static struct library_tab_item
library_tab_resolve (const char *raw)
{
struct library_tab_item item;
item.type = *raw++;
item.name = raw;
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)
{
(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;
switch (x->type)
switch (x.type)
{
case LIBRARY_ROOT: prefix = "/"; name = ""; break;
case LIBRARY_UP: prefix = "/"; name = ".."; break;
case LIBRARY_DIR: prefix = "/"; name = x->name; break;
case LIBRARY_FILE: prefix = " "; name = x->name; break;
case LIBRARY_ROOT: prefix = "/"; name = ""; break;
case LIBRARY_UP: prefix = "/"; name = ".."; break;
case LIBRARY_DIR: prefix = "/"; name = x.name; break;
case LIBRARY_FILE: prefix = " "; name = x.name; break;
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);
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
@ -2490,38 +2452,31 @@ library_tab_header_type (const char *key)
{
if (!strcasecmp_ascii (key, "file")) return LIBRARY_FILE;
if (!strcasecmp_ascii (key, "directory")) return LIBRARY_DIR;
if (!strcasecmp_ascii (key, "playlist")) return LIBRARY_PLAYLIST;
return 0;
}
static void
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 *title = str_map_find (map, "title");
char *name = (artist && title)
? xstrdup_printf ("%s - %s", artist, title)
: xstrdup (xbasename (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);
library_tab_add (type, name, path);
free (name);
}
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)
return a->type - b->type;
struct library_tab_item xa = library_tab_resolve (*a);
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 *
@ -2589,25 +2544,16 @@ library_tab_change_level (const char *new_path)
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
library_tab_load_data (const struct strv *data)
{
library_tab_reset ();
strv_reset (&g_library_tab.items);
char *parent = library_tab_parent ();
if (parent)
{
library_tab_add (LIBRARY_ROOT, -1, "", "");
library_tab_add (LIBRARY_UP, -1, "", parent);
library_tab_add (LIBRARY_ROOT, "", "");
library_tab_add (LIBRARY_UP, "", parent);
free (parent);
}
@ -2627,16 +2573,16 @@ library_tab_load_data (const struct strv *data)
}
str_map_free (&map);
struct library_tab_item *items = g_library_tab.items;
size_t len = g_library_tab.super.item_count = g_library_tab.items_len;
qsort (items, len, sizeof *items,
struct strv *items = &g_library_tab.items;
qsort (items->vector, items->len, sizeof *items->vector,
(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
g_library_tab.super.item_mark = -1;
// 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_invalidate ();
@ -2724,8 +2670,9 @@ library_tab_is_range_playable (struct tab_range range)
{
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item *x = &g_library_tab.items[i];
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
return true;
}
return false;
@ -2743,7 +2690,9 @@ library_tab_on_action (enum action action)
if (range.from < 0)
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)
{
case ACTION_CHOOSE:
@ -2751,12 +2700,12 @@ library_tab_on_action (enum action action)
if (range.from != range.upto)
break;
switch (x->type)
switch (x.type)
{
case LIBRARY_ROOT:
case LIBRARY_UP:
case LIBRARY_DIR: library_tab_reload (x->path); break;
case LIBRARY_FILE: MPD_SIMPLE ("add", x->path); break;
case LIBRARY_DIR: library_tab_reload (x.path); break;
case LIBRARY_FILE: MPD_SIMPLE ("add", x.path); break;
default: hard_assert (!"invalid item type");
}
tab->item_mark = -1;
@ -2805,9 +2754,10 @@ library_tab_on_action (enum action action)
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item *x = &g_library_tab.items[i];
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
MPD_SIMPLE ("add", x->path);
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
MPD_SIMPLE ("add", x.path);
}
tab->item_mark = -1;
return true;
@ -2823,9 +2773,10 @@ library_tab_on_action (enum action action)
mpd_client_send_command (c, "clear", NULL);
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item *x = &g_library_tab.items[i];
if (x->type == LIBRARY_DIR || x->type == LIBRARY_FILE)
mpd_client_send_command (c, "add", x->path, NULL);
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
mpd_client_send_command (c, "add", x.path, NULL);
}
if (g.state == PLAYER_PLAYING)
mpd_client_send_command (c, "play", NULL);
@ -2845,7 +2796,7 @@ static struct tab *
library_tab_init (void)
{
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;
tab_init (super, "Library");
@ -3401,6 +3352,26 @@ debug_tab_init (void)
// --- 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
mpd_update_playlist_time (void)
{