2016-09-28 22:44:59 +02:00
|
|
|
/*
|
|
|
|
* nncmpp -- the MPD client you never knew you needed
|
|
|
|
*
|
2017-01-24 00:18:03 +01:00
|
|
|
* Copyright (c) 2016 - 2017, Přemysl Janouch <p.janouch@gmail.com>
|
2016-09-28 22:44:59 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-10-03 19:43:51 +02:00
|
|
|
// We "need" to have an enum for attributes before including liberty.
|
|
|
|
// Avoiding colours in the defaults here in order to support dumb terminals.
|
|
|
|
#define ATTRIBUTE_TABLE(XX) \
|
2017-01-29 17:55:50 +01:00
|
|
|
XX( NORMAL, "normal", -1, -1, 0 ) \
|
2016-10-03 19:43:51 +02:00
|
|
|
XX( HIGHLIGHT, "highlight", -1, -1, A_BOLD ) \
|
|
|
|
/* Gauge */ \
|
|
|
|
XX( ELAPSED, "elapsed", -1, -1, A_REVERSE ) \
|
|
|
|
XX( REMAINS, "remains", -1, -1, A_UNDERLINE ) \
|
|
|
|
/* Tab bar */ \
|
|
|
|
XX( TAB_BAR, "tab_bar", -1, -1, A_REVERSE ) \
|
|
|
|
XX( TAB_ACTIVE, "tab_active", -1, -1, A_UNDERLINE ) \
|
|
|
|
/* Listview */ \
|
2017-06-05 00:56:34 +02:00
|
|
|
XX( HEADER, "header", -1, -1, A_UNDERLINE ) \
|
2016-10-03 19:43:51 +02:00
|
|
|
XX( EVEN, "even", -1, -1, 0 ) \
|
|
|
|
XX( ODD, "odd", -1, -1, 0 ) \
|
2017-06-04 02:51:49 +02:00
|
|
|
XX( DIRECTORY, "directory", -1, -1, 0 ) \
|
2016-10-03 19:43:51 +02:00
|
|
|
XX( SELECTION, "selection", -1, -1, A_REVERSE ) \
|
|
|
|
XX( SCROLLBAR, "scrollbar", -1, -1, 0 ) \
|
|
|
|
/* These are for debugging only */ \
|
|
|
|
XX( WARNING, "warning", 3, -1, 0 ) \
|
|
|
|
XX( ERROR, "error", 1, -1, 0 ) \
|
|
|
|
XX( INCOMING, "incoming", 2, -1, 0 ) \
|
|
|
|
XX( OUTGOING, "outgoing", 4, -1, 0 )
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
#define XX(name, config, fg_, bg_, attrs_) ATTRIBUTE_ ## name,
|
|
|
|
ATTRIBUTE_TABLE (XX)
|
|
|
|
#undef XX
|
|
|
|
ATTRIBUTE_COUNT
|
|
|
|
};
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// My battle-tested C framework acting as a GLib replacement. Its one big
|
|
|
|
// disadvantage is missing support for i18n but that can eventually be added
|
|
|
|
// as an optional feature. Localised applications look super awkward, though.
|
|
|
|
|
2016-10-03 19:43:51 +02:00
|
|
|
// User data for logger functions to enable formatted logging
|
|
|
|
#define print_fatal_data ((void *) ATTRIBUTE_ERROR)
|
|
|
|
#define print_error_data ((void *) ATTRIBUTE_ERROR)
|
|
|
|
#define print_warning_data ((void *) ATTRIBUTE_WARNING)
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
#define LIBERTY_WANT_POLLER
|
|
|
|
#define LIBERTY_WANT_ASYNC
|
2016-10-09 09:48:47 +02:00
|
|
|
#define LIBERTY_WANT_PROTO_HTTP
|
2016-10-11 09:40:01 +02:00
|
|
|
#define LIBERTY_WANT_PROTO_MPD
|
2016-09-28 22:44:59 +02:00
|
|
|
#include "liberty/liberty.c"
|
2017-01-23 23:32:30 +01:00
|
|
|
#include "liberty/liberty-tui.c"
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#ifndef TIOCGWINSZ
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif // ! TIOCGWINSZ
|
|
|
|
|
|
|
|
// ncurses is notoriously retarded for input handling, we need something
|
|
|
|
// different if only to receive mouse events reliably.
|
|
|
|
|
|
|
|
#include "termo.h"
|
|
|
|
|
2016-10-06 03:27:29 +02:00
|
|
|
// We need cURL to extract links from Internet stream playlists. It'd be way
|
|
|
|
// too much code to do this all by ourselves, and there's nothing better around.
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-01 23:43:51 +02:00
|
|
|
#define APP_TITLE PROGRAM_NAME ///< Left top corner
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
// --- Utilities ---------------------------------------------------------------
|
|
|
|
|
|
|
|
// The standard endwin/refresh sequence makes the terminal flicker
|
|
|
|
static void
|
|
|
|
update_curses_terminal_size (void)
|
|
|
|
{
|
|
|
|
#if defined (HAVE_RESIZETERM) && defined (TIOCGWINSZ)
|
|
|
|
struct winsize size;
|
|
|
|
if (!ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &size))
|
|
|
|
{
|
|
|
|
char *row = getenv ("LINES");
|
|
|
|
char *col = getenv ("COLUMNS");
|
|
|
|
unsigned long tmp;
|
|
|
|
resizeterm (
|
|
|
|
(row && xstrtoul (&tmp, row, 10)) ? tmp : size.ws_row,
|
|
|
|
(col && xstrtoul (&tmp, col, 10)) ? tmp : size.ws_col);
|
|
|
|
}
|
|
|
|
#else // HAVE_RESIZETERM && TIOCGWINSZ
|
|
|
|
endwin ();
|
|
|
|
refresh ();
|
|
|
|
#endif // HAVE_RESIZETERM && TIOCGWINSZ
|
|
|
|
}
|
|
|
|
|
2016-10-04 18:33:14 +02:00
|
|
|
static int64_t
|
|
|
|
clock_msec (clockid_t clock)
|
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
hard_assert (clock_gettime (clock, &tp) != -1);
|
|
|
|
return (int64_t) tp.tv_sec * 1000 + (int64_t) tp.tv_nsec / 1000000;
|
|
|
|
}
|
|
|
|
|
2016-10-10 16:02:57 +02:00
|
|
|
static bool
|
|
|
|
xstrtoul_map (const struct str_map *map, const char *key, unsigned long *out)
|
|
|
|
{
|
|
|
|
const char *field = str_map_find (map, key);
|
|
|
|
return field && xstrtoul (out, field, 10);
|
|
|
|
}
|
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
static const char *
|
|
|
|
xbasename (const char *path)
|
|
|
|
{
|
|
|
|
const char *last_slash = strrchr (path, '/');
|
|
|
|
return last_slash ? last_slash + 1 : path;
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:39:31 +02:00
|
|
|
static char *
|
|
|
|
latin1_to_utf8 (const char *latin1)
|
|
|
|
{
|
|
|
|
struct str converted;
|
|
|
|
str_init (&converted);
|
|
|
|
while (*latin1)
|
|
|
|
{
|
|
|
|
uint8_t c = *latin1++;
|
|
|
|
if (c < 0x80)
|
|
|
|
str_append_c (&converted, c);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str_append_c (&converted, 0xC0 | (c >> 6));
|
|
|
|
str_append_c (&converted, 0x80 | (c & 0x3F));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str_steal (&converted);
|
|
|
|
}
|
|
|
|
|
2017-06-05 13:05:33 +02:00
|
|
|
static void
|
|
|
|
cstr_uncapitalize (char *s)
|
|
|
|
{
|
|
|
|
if (isupper (s[0]) && islower (s[1]))
|
|
|
|
s[0] = tolower_ascii (s[0]);
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:28:40 +02:00
|
|
|
static int
|
|
|
|
print_curl_debug (CURL *easy, curl_infotype type, char *data, size_t len,
|
|
|
|
void *ud)
|
|
|
|
{
|
|
|
|
(void) easy;
|
|
|
|
(void) ud;
|
|
|
|
(void) type;
|
|
|
|
|
|
|
|
char copy[len + 1];
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
uint8_t c = data[i];
|
|
|
|
copy[i] = c >= 32 || c == '\n' ? c : '.';
|
|
|
|
}
|
|
|
|
copy[len] = '\0';
|
|
|
|
|
|
|
|
char *next;
|
|
|
|
for (char *p = copy; p; p = next)
|
|
|
|
{
|
|
|
|
if ((next = strchr (p, '\n')))
|
|
|
|
*next++ = '\0';
|
|
|
|
if (!*p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!utf8_validate (p, strlen (p)))
|
|
|
|
{
|
|
|
|
char *fixed = latin1_to_utf8 (p);
|
|
|
|
print_debug ("cURL: %s", fixed);
|
|
|
|
free (fixed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
print_debug ("cURL: %s", p);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 01:48:45 +02:00
|
|
|
static char *
|
|
|
|
mpd_parse_kv (char *line, char **value)
|
|
|
|
{
|
|
|
|
char *key = mpd_client_parse_kv (line, value);
|
|
|
|
if (!key) print_debug ("%s: %s", "erroneous MPD output", line);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2016-10-10 07:45:17 +02:00
|
|
|
// --- cURL async wrapper ------------------------------------------------------
|
|
|
|
|
|
|
|
// You are meant to subclass this structure, no user_data pointers needed
|
|
|
|
struct poller_curl_task;
|
|
|
|
|
|
|
|
/// Receives notification for finished transfers
|
|
|
|
typedef void (*poller_curl_done_fn)
|
|
|
|
(CURLMsg *msg, struct poller_curl_task *task);
|
|
|
|
|
|
|
|
struct poller_curl_task
|
|
|
|
{
|
|
|
|
CURL *easy; ///< cURL easy interface handle
|
|
|
|
char curl_error[CURL_ERROR_SIZE]; ///< cURL error info buffer
|
|
|
|
poller_curl_done_fn on_done; ///< Done callback
|
|
|
|
};
|
|
|
|
|
|
|
|
struct poller_curl_fd
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct poller_curl_fd)
|
|
|
|
struct poller_fd fd; ///< Poller FD
|
|
|
|
};
|
|
|
|
|
|
|
|
struct poller_curl
|
|
|
|
{
|
|
|
|
struct poller *poller; ///< Parent poller
|
|
|
|
struct poller_timer timer; ///< cURL timer
|
|
|
|
CURLM *multi; ///< cURL multi interface handle
|
|
|
|
struct poller_curl_fd *fds; ///< List of all FDs
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
poller_curl_collect (struct poller_curl *self, curl_socket_t s, int ev_bitmask)
|
|
|
|
{
|
|
|
|
int running = 0;
|
|
|
|
CURLMcode res;
|
|
|
|
// XXX: ignoring errors, in particular CURLM_CALL_MULTI_PERFORM
|
|
|
|
if ((res = curl_multi_socket_action (self->multi, s, ev_bitmask, &running)))
|
|
|
|
print_debug ("cURL: %s", curl_multi_strerror (res));
|
|
|
|
|
|
|
|
CURLMsg *msg;
|
|
|
|
while ((msg = curl_multi_info_read (self->multi, &running)))
|
|
|
|
if (msg->msg == CURLMSG_DONE)
|
|
|
|
{
|
|
|
|
struct poller_curl_task *task = NULL;
|
|
|
|
hard_assert (!curl_easy_getinfo
|
|
|
|
(msg->easy_handle, CURLINFO_PRIVATE, &task));
|
|
|
|
task->on_done (msg, task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
poller_curl_on_socket (const struct pollfd *pfd, void *user_data)
|
|
|
|
{
|
|
|
|
int mask = 0;
|
|
|
|
if (pfd->revents & POLLIN) mask |= CURL_CSELECT_IN;
|
|
|
|
if (pfd->revents & POLLOUT) mask |= CURL_CSELECT_OUT;
|
|
|
|
if (pfd->revents & POLLERR) mask |= CURL_CSELECT_ERR;
|
|
|
|
poller_curl_collect (user_data, pfd->fd, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
poller_curl_on_socket_action (CURL *easy, curl_socket_t s, int what,
|
|
|
|
void *user_data, void *socket_data)
|
|
|
|
{
|
|
|
|
(void) easy;
|
|
|
|
struct poller_curl *self = user_data;
|
|
|
|
|
|
|
|
struct poller_curl_fd *fd;
|
|
|
|
if (!(fd = socket_data))
|
|
|
|
{
|
|
|
|
fd = xmalloc (sizeof *fd);
|
|
|
|
LIST_PREPEND (self->fds, fd);
|
|
|
|
|
|
|
|
poller_fd_init (&fd->fd, self->poller, s);
|
|
|
|
fd->fd.dispatcher = poller_curl_on_socket;
|
|
|
|
fd->fd.user_data = self;
|
|
|
|
curl_multi_assign (self->multi, s, fd);
|
|
|
|
}
|
|
|
|
if (what == CURL_POLL_REMOVE)
|
|
|
|
{
|
|
|
|
poller_fd_reset (&fd->fd);
|
|
|
|
LIST_UNLINK (self->fds, fd);
|
|
|
|
free (fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
short events = 0;
|
|
|
|
if (what == CURL_POLL_IN) events = POLLIN;
|
|
|
|
if (what == CURL_POLL_OUT) events = POLLOUT;
|
|
|
|
if (what == CURL_POLL_INOUT) events = POLLIN | POLLOUT;
|
|
|
|
poller_fd_set (&fd->fd, events);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
poller_curl_on_timer (void *user_data)
|
|
|
|
{
|
|
|
|
poller_curl_collect (user_data, CURL_SOCKET_TIMEOUT, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
poller_curl_on_timer_change (CURLM *multi, long timeout_ms, void *user_data)
|
|
|
|
{
|
|
|
|
(void) multi;
|
|
|
|
struct poller_curl *self = user_data;
|
|
|
|
|
|
|
|
if (timeout_ms < 0)
|
|
|
|
poller_timer_reset (&self->timer);
|
|
|
|
else
|
|
|
|
poller_timer_set (&self->timer, timeout_ms);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static bool
|
|
|
|
poller_curl_init (struct poller_curl *self, struct poller *poller,
|
|
|
|
struct error **e)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
if (!(self->multi = curl_multi_init ()))
|
2016-10-10 07:47:40 +02:00
|
|
|
return error_set (e, "cURL setup failed");
|
2016-10-10 07:45:17 +02:00
|
|
|
|
|
|
|
CURLMcode mres;
|
|
|
|
if ((mres = curl_multi_setopt (self->multi,
|
|
|
|
CURLMOPT_SOCKETFUNCTION, poller_curl_on_socket_action))
|
|
|
|
|| (mres = curl_multi_setopt (self->multi,
|
|
|
|
CURLMOPT_TIMERFUNCTION, poller_curl_on_timer_change))
|
|
|
|
|| (mres = curl_multi_setopt (self->multi, CURLMOPT_SOCKETDATA, self))
|
|
|
|
|| (mres = curl_multi_setopt (self->multi, CURLMOPT_TIMERDATA, self)))
|
|
|
|
{
|
|
|
|
curl_multi_cleanup (self->multi);
|
2016-10-10 07:47:40 +02:00
|
|
|
return error_set (e, "%s: %s",
|
|
|
|
"cURL setup failed", curl_multi_strerror (mres));
|
2016-10-10 07:45:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
poller_timer_init (&self->timer, (self->poller = poller));
|
|
|
|
self->timer.dispatcher = poller_curl_on_timer;
|
|
|
|
self->timer.user_data = self;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
poller_curl_free (struct poller_curl *self)
|
|
|
|
{
|
|
|
|
curl_multi_cleanup (self->multi);
|
|
|
|
poller_timer_reset (&self->timer);
|
|
|
|
|
|
|
|
LIST_FOR_EACH (struct poller_curl_fd, iter, self->fds)
|
|
|
|
{
|
|
|
|
poller_fd_reset (&iter->fd);
|
|
|
|
free (iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
/// Initialize a task with a new easy instance that can be used with the poller
|
|
|
|
static bool
|
|
|
|
poller_curl_spawn (struct poller_curl_task *task, struct error **e)
|
|
|
|
{
|
|
|
|
CURL *easy;
|
|
|
|
if (!(easy = curl_easy_init ()))
|
2016-10-10 07:47:40 +02:00
|
|
|
return error_set (e, "cURL setup failed");
|
2016-10-10 07:45:17 +02:00
|
|
|
|
|
|
|
// We already take care of SIGPIPE, and native DNS timeouts are only
|
|
|
|
// a problem for people without the AsynchDNS feature.
|
|
|
|
//
|
|
|
|
// Unfortunately, cURL doesn't allow custom callbacks for DNS.
|
|
|
|
// The most we could try is parse out the hostname and provide an address
|
|
|
|
// override for it using CURLOPT_RESOLVE. Or be our own SOCKS4A/5 proxy.
|
|
|
|
|
|
|
|
CURLcode res;
|
|
|
|
if ((res = curl_easy_setopt (easy, CURLOPT_NOSIGNAL, 1L))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_ERRORBUFFER, task->curl_error))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_PRIVATE, task)))
|
|
|
|
{
|
|
|
|
curl_easy_cleanup (easy);
|
2016-10-10 07:47:40 +02:00
|
|
|
return error_set (e, "%s", curl_easy_strerror (res));
|
2016-10-10 07:45:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
task->easy = easy;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
poller_curl_add (struct poller_curl *self, CURL *easy, struct error **e)
|
|
|
|
{
|
|
|
|
CURLMcode mres;
|
|
|
|
// "CURLMOPT_TIMERFUNCTION [...] will be called from within this function"
|
2016-10-10 07:47:40 +02:00
|
|
|
if ((mres = curl_multi_add_handle (self->multi, easy)))
|
|
|
|
return error_set (e, "%s", curl_multi_strerror (mres));
|
|
|
|
return true;
|
2016-10-10 07:45:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
poller_curl_remove (struct poller_curl *self, CURL *easy, struct error **e)
|
|
|
|
{
|
|
|
|
CURLMcode mres;
|
2016-10-10 07:47:40 +02:00
|
|
|
if ((mres = curl_multi_remove_handle (self->multi, easy)))
|
|
|
|
return error_set (e, "%s", curl_multi_strerror (mres));
|
|
|
|
return true;
|
2016-10-10 07:45:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 13:11:17 +02:00
|
|
|
// --- Compact map -------------------------------------------------------------
|
|
|
|
|
|
|
|
// MPD provides us with a hefty amount of little key-value maps. The overhead
|
|
|
|
// of str_map for such constant (string -> string) maps is too high and it's
|
|
|
|
// much better to serialize them (mainly cache locality and memory efficiency).
|
|
|
|
//
|
|
|
|
// This isn't intended to be reusable and has case insensitivity built-in.
|
|
|
|
|
|
|
|
typedef uint8_t *compact_map_t; ///< Compacted (string -> string) map
|
|
|
|
|
|
|
|
static compact_map_t
|
|
|
|
compact_map (struct str_map *map)
|
|
|
|
{
|
|
|
|
struct str s;
|
|
|
|
str_init (&s);
|
|
|
|
struct str_map_iter iter;
|
|
|
|
str_map_iter_init (&iter, map);
|
|
|
|
|
|
|
|
char *value;
|
|
|
|
static const size_t zero = 0, alignment = sizeof zero;
|
|
|
|
while ((value = str_map_iter_next (&iter)))
|
|
|
|
{
|
|
|
|
size_t entry_len = iter.link->key_length + 1 + strlen (value) + 1;
|
|
|
|
size_t padding_len = (alignment - entry_len % alignment) % alignment;
|
|
|
|
entry_len += padding_len;
|
|
|
|
|
|
|
|
str_append_data (&s, &entry_len, sizeof entry_len);
|
|
|
|
str_append_printf (&s, "%s%c%s%c", iter.link->key, 0, value, 0);
|
|
|
|
str_append_data (&s, &zero, padding_len);
|
|
|
|
}
|
|
|
|
str_append_data (&s, &zero, sizeof zero);
|
|
|
|
return (compact_map_t) str_steal (&s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
compact_map_find (compact_map_t data, const char *needle)
|
|
|
|
{
|
|
|
|
size_t entry_len;
|
|
|
|
while ((entry_len = *(size_t *) data))
|
|
|
|
{
|
|
|
|
data += sizeof entry_len;
|
|
|
|
if (!strcasecmp_ascii (needle, (const char *) data))
|
|
|
|
return (char *) data + strlen (needle) + 1;
|
|
|
|
data += entry_len;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
struct item_list
|
|
|
|
{
|
|
|
|
compact_map_t *items; ///< Compacted (string -> string) maps
|
|
|
|
size_t len; ///< Length
|
|
|
|
size_t alloc; ///< Allocated items
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_list_init (struct item_list *self)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
self->items = xcalloc (sizeof *self->items, (self->alloc = 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_list_free (struct item_list *self)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < self->len; i++)
|
|
|
|
free (self->items[i]);
|
|
|
|
free (self->items);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
item_list_set (struct item_list *self, int i, struct str_map *item)
|
|
|
|
{
|
|
|
|
if (i < 0 || (size_t) i >= self->len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
free (self->items[i]);
|
|
|
|
self->items[i] = compact_map (item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static compact_map_t
|
|
|
|
item_list_get (struct item_list *self, int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || (size_t) i >= self->len || !self->items[i])
|
|
|
|
return false;
|
|
|
|
return self->items[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_list_resize (struct item_list *self, size_t len)
|
|
|
|
{
|
|
|
|
// Make the allocated array big enough but not too large
|
|
|
|
size_t new_alloc = self->alloc;
|
|
|
|
while (new_alloc < len)
|
|
|
|
new_alloc <<= 1;
|
|
|
|
while ((new_alloc >> 1) >= len
|
|
|
|
&& (new_alloc - len) >= 1024)
|
|
|
|
new_alloc >>= 1;
|
|
|
|
|
|
|
|
for (size_t i = len; i < self->len; i++)
|
|
|
|
free (self->items[i]);
|
|
|
|
if (new_alloc != self->alloc)
|
|
|
|
self->items = xreallocarray (self->items,
|
|
|
|
sizeof *self->items, (self->alloc = new_alloc));
|
|
|
|
for (size_t i = self->len; i < len; i++)
|
|
|
|
self->items[i] = NULL;
|
|
|
|
|
|
|
|
self->len = len;
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// --- Application -------------------------------------------------------------
|
|
|
|
|
|
|
|
// Function names are prefixed mostly because of curses which clutters the
|
|
|
|
// global namespace and makes it harder to distinguish what functions relate to.
|
|
|
|
|
|
|
|
// The user interface is focused on conceptual simplicity. That is important
|
|
|
|
// since we're not using any TUI framework (which are mostly a lost cause to me
|
|
|
|
// in the post-Unicode era and not worth pursuing), and the code would get
|
2017-01-27 23:53:09 +01:00
|
|
|
// bloated and incomprehensible fast. We mostly rely on "row_buffer" to write
|
|
|
|
// text from left to right row after row while keeping track of cells.
|
2016-09-28 22:44:59 +02:00
|
|
|
//
|
|
|
|
// There is an independent top pane displaying general status information,
|
|
|
|
// followed by a tab bar and a listview served by a per-tab event handler.
|
|
|
|
//
|
|
|
|
// For simplicity, the listview can only work with items that are one row high.
|
|
|
|
|
|
|
|
struct tab;
|
|
|
|
struct row_buffer;
|
2016-10-12 16:06:23 +02:00
|
|
|
enum action;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 05:58:19 +02:00
|
|
|
/// Try to handle an action in the tab
|
2016-10-12 16:06:23 +02:00
|
|
|
typedef bool (*tab_action_fn) (enum action action);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
/// Draw an item to the screen using the row buffer API
|
2016-10-05 05:58:19 +02:00
|
|
|
typedef void (*tab_item_draw_fn)
|
|
|
|
(size_t item_index, struct row_buffer *buffer, int width);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
struct tab
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct tab)
|
|
|
|
|
|
|
|
char *name; ///< Visible identifier
|
|
|
|
size_t name_width; ///< Visible width of the name
|
|
|
|
|
2017-06-05 00:56:34 +02:00
|
|
|
char *header; ///< The header, should there be any
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// Implementation:
|
|
|
|
|
|
|
|
// TODO: free() callback?
|
2016-10-05 05:58:19 +02:00
|
|
|
tab_action_fn on_action; ///< User action handler callback
|
2016-09-28 22:44:59 +02:00
|
|
|
tab_item_draw_fn on_item_draw; ///< Item draw callback
|
|
|
|
|
|
|
|
// Provided by tab owner:
|
|
|
|
|
|
|
|
bool can_multiselect; ///< Multiple items can be selected
|
|
|
|
size_t item_count; ///< Total item count
|
|
|
|
|
|
|
|
// Managed by the common handler:
|
|
|
|
|
|
|
|
int item_top; ///< Index of the topmost item
|
|
|
|
int item_selected; ///< Index of the selected item
|
|
|
|
};
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
enum player_state { PLAYER_STOPPED, PLAYER_PLAYING, PLAYER_PAUSED };
|
|
|
|
|
|
|
|
// Basically a container for most of the globals; no big sense in handing
|
|
|
|
// around a pointer to this, hence it is a simple global variable as well.
|
|
|
|
// There is enough global state as it is.
|
|
|
|
|
|
|
|
static struct app_context
|
|
|
|
{
|
|
|
|
// Event loop:
|
|
|
|
|
|
|
|
struct poller poller; ///< Poller
|
|
|
|
bool quitting; ///< Quit signal for the event loop
|
|
|
|
bool polling; ///< The event loop is running
|
|
|
|
|
|
|
|
struct poller_fd tty_event; ///< Terminal input event
|
|
|
|
struct poller_fd signal_event; ///< Signal FD event
|
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
struct poller_timer message_timer; ///< Message timeout
|
|
|
|
char *message; ///< Message to show in the statusbar
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// Connection:
|
|
|
|
|
|
|
|
struct mpd_client client; ///< MPD client interface
|
2016-10-02 01:15:52 +02:00
|
|
|
struct poller_timer connect_event; ///< MPD reconnect timer
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
enum player_state state; ///< Player state
|
2016-10-05 01:49:06 +02:00
|
|
|
struct str_map playback_info; ///< Current song info
|
2016-10-01 21:20:07 +02:00
|
|
|
|
|
|
|
struct poller_timer elapsed_event; ///< Seconds elapsed event
|
2016-10-04 18:33:14 +02:00
|
|
|
int64_t elapsed_since; ///< Time of the next tick
|
|
|
|
|
2016-10-01 21:20:07 +02:00
|
|
|
// TODO: initialize these to -1
|
2016-10-10 16:02:57 +02:00
|
|
|
int song; ///< Current song index
|
2016-10-01 21:20:07 +02:00
|
|
|
int song_elapsed; ///< Song elapsed in seconds
|
|
|
|
int song_duration; ///< Song duration in seconds
|
2016-10-01 23:43:51 +02:00
|
|
|
int volume; ///< Current volume
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-12 13:11:17 +02:00
|
|
|
struct item_list playlist; ///< Current playlist
|
2016-10-10 16:02:57 +02:00
|
|
|
uint32_t playlist_version; ///< Playlist version
|
2017-06-05 18:50:50 +02:00
|
|
|
int playlist_time; ///< Play time in seconds
|
2016-10-10 16:02:57 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// Data:
|
|
|
|
|
|
|
|
struct config config; ///< Program configuration
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv streams; ///< List of "name NUL URI NUL"
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-04 07:10:08 +02:00
|
|
|
struct tab *help_tab; ///< Special help tab
|
|
|
|
struct tab *tabs; ///< All other tabs
|
2016-09-28 22:44:59 +02:00
|
|
|
struct tab *active_tab; ///< Active tab
|
2016-10-10 17:07:58 +02:00
|
|
|
struct tab *last_tab; ///< Previous tab
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-02 04:09:54 +02:00
|
|
|
// Emulated widgets:
|
|
|
|
|
2016-10-04 23:39:32 +02:00
|
|
|
int header_height; ///< Height of the header
|
2016-10-02 04:09:54 +02:00
|
|
|
|
2017-06-05 00:56:34 +02:00
|
|
|
int tabs_offset; ///< Offset to tabs or -1
|
2016-10-02 04:09:54 +02:00
|
|
|
int controls_offset; ///< Offset to player controls or -1
|
|
|
|
int gauge_offset; ///< Offset to the gauge or -1
|
|
|
|
int gauge_width; ///< Width of the gauge, if present
|
|
|
|
|
2016-10-04 23:39:32 +02:00
|
|
|
struct poller_idle refresh_event; ///< Refresh the screen
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// Terminal:
|
|
|
|
|
|
|
|
termo_t *tk; ///< termo handle
|
|
|
|
struct poller_timer tk_timer; ///< termo timeout timer
|
|
|
|
bool locale_is_utf8; ///< The locale is Unicode
|
2016-10-03 06:01:28 +02:00
|
|
|
bool use_partial_boxes; ///< Use Unicode box drawing chars
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
struct attrs attrs[ATTRIBUTE_COUNT];
|
|
|
|
}
|
2017-01-29 15:26:55 +01:00
|
|
|
g;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
/// Shortcut to retrieve named terminal attributes
|
2017-01-29 15:26:55 +01:00
|
|
|
#define APP_ATTR(name) g.attrs[ATTRIBUTE_ ## name].attrs
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
tab_init (struct tab *self, const char *name)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
|
|
|
// Add some padding for decorative purposes
|
|
|
|
self->name = xstrdup_printf (" %s ", name);
|
|
|
|
// Assuming tab names are pure ASCII, otherwise this would be inaccurate
|
|
|
|
// and we'd need to filter it first to replace invalid chars with '?'
|
|
|
|
self->name_width = u8_strwidth ((uint8_t *) self->name, locale_charset ());
|
2017-06-04 00:05:20 +02:00
|
|
|
self->item_selected = 0;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tab_free (struct tab *self)
|
|
|
|
{
|
|
|
|
free (self->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Configuration -----------------------------------------------------------
|
|
|
|
|
|
|
|
static struct config_schema g_config_settings[] =
|
|
|
|
{
|
|
|
|
{ .name = "address",
|
|
|
|
.comment = "Address to connect to the MPD server",
|
|
|
|
.type = CONFIG_ITEM_STRING,
|
2016-11-20 23:14:47 +01:00
|
|
|
.default_ = "\"localhost\"" },
|
2016-09-28 22:44:59 +02:00
|
|
|
{ .name = "password",
|
|
|
|
.comment = "Password to use for MPD authentication",
|
|
|
|
.type = CONFIG_ITEM_STRING },
|
|
|
|
{ .name = "root",
|
|
|
|
.comment = "Where all the files MPD is playing are located",
|
|
|
|
.type = CONFIG_ITEM_STRING },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct config_schema g_config_colors[] =
|
|
|
|
{
|
|
|
|
#define XX(name_, config, fg_, bg_, attrs_) \
|
|
|
|
{ .name = config, .type = CONFIG_ITEM_STRING },
|
|
|
|
ATTRIBUTE_TABLE (XX)
|
|
|
|
#undef XX
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_config_string (struct config_item *root, const char *key)
|
|
|
|
{
|
|
|
|
struct config_item *item = config_item_get (root, key, NULL);
|
|
|
|
hard_assert (item);
|
|
|
|
if (item->type == CONFIG_ITEM_NULL)
|
|
|
|
return NULL;
|
|
|
|
hard_assert (config_item_type_is_string (item->type));
|
|
|
|
return item->value.string.str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_config_settings (struct config_item *subtree, void *user_data)
|
|
|
|
{
|
|
|
|
config_schema_apply_to_object (g_config_settings, subtree, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_config_colors (struct config_item *subtree, void *user_data)
|
|
|
|
{
|
|
|
|
config_schema_apply_to_object (g_config_colors, subtree, user_data);
|
|
|
|
|
|
|
|
// The attributes cannot be changed dynamically right now, so it doesn't
|
|
|
|
// make much sense to make use of "on_change" callbacks either.
|
|
|
|
// For simplicity, we should reload the entire table on each change anyway.
|
2017-01-23 23:32:30 +01:00
|
|
|
const char *value;
|
2016-09-28 22:44:59 +02:00
|
|
|
#define XX(name, config, fg_, bg_, attrs_) \
|
2017-01-23 23:32:30 +01:00
|
|
|
if ((value = get_config_string (subtree, config))) \
|
2017-01-29 15:26:55 +01:00
|
|
|
g.attrs[ATTRIBUTE_ ## name] = attrs_decode (value);
|
2016-09-28 22:44:59 +02:00
|
|
|
ATTRIBUTE_TABLE (XX)
|
|
|
|
#undef XX
|
|
|
|
}
|
|
|
|
|
2016-10-23 18:35:15 +02:00
|
|
|
static int
|
|
|
|
app_casecmp (const uint8_t *a, const uint8_t *b)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
// XXX: this seems to produce some strange results
|
|
|
|
if (u8_casecmp (a, strlen ((const char *) a), b, strlen ((const char *) b),
|
|
|
|
NULL, NULL, &res))
|
|
|
|
res = u8_strcmp (a, b);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:54:40 +02:00
|
|
|
static int
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_sort_utf8_cb (const void *a, const void *b)
|
2016-10-05 05:54:40 +02:00
|
|
|
{
|
2016-10-23 18:35:15 +02:00
|
|
|
return app_casecmp (*(const uint8_t **) a, *(const uint8_t **) b);
|
2016-10-05 05:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_config_streams (struct config_item *subtree, void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
// XXX: we can't use the tab in load_config_streams() because it hasn't
|
|
|
|
// been initialized yet, and we cannot initialize it before the
|
|
|
|
// configuration has been loaded. Thus we load it into the app_context.
|
|
|
|
struct str_map_iter iter;
|
|
|
|
str_map_iter_init (&iter, &subtree->value.object);
|
|
|
|
struct config_item *item;
|
|
|
|
while ((item = str_map_iter_next (&iter)))
|
|
|
|
if (!config_item_type_is_string (item->type))
|
|
|
|
print_warning ("`%s': stream URIs must be strings", iter.link->key);
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
strv_append_owned (&g.streams, xstrdup_printf ("%s%c%s",
|
2016-10-12 13:11:17 +02:00
|
|
|
iter.link->key, 0, item->value.string.str));
|
2016-10-05 05:54:40 +02:00
|
|
|
}
|
2017-01-29 15:26:55 +01:00
|
|
|
qsort (g.streams.vector, g.streams.len,
|
|
|
|
sizeof *g.streams.vector, strv_sort_utf8_cb);
|
2016-10-05 05:54:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
|
|
|
app_load_configuration (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct config *config = &g.config;
|
2016-09-28 22:44:59 +02:00
|
|
|
config_register_module (config, "settings", load_config_settings, NULL);
|
|
|
|
config_register_module (config, "colors", load_config_colors, NULL);
|
2016-10-05 05:54:40 +02:00
|
|
|
config_register_module (config, "streams", load_config_streams, NULL);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-11-20 23:14:47 +01:00
|
|
|
// Bootstrap configuration, so that we can access schema items at all
|
|
|
|
config_load (config, config_item_object ());
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
char *filename = resolve_filename
|
|
|
|
(PROGRAM_NAME ".conf", resolve_relative_config_filename);
|
|
|
|
if (!filename)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct error *e = NULL;
|
|
|
|
struct config_item *root = config_read_from_file (filename, &e);
|
|
|
|
free (filename);
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
print_error ("error loading configuration: %s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (root)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
config_load (&g.config, root);
|
|
|
|
config_schema_call_changed (g.config.root);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Application -------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_init_attributes (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
#define XX(name, config, fg_, bg_, attrs_) \
|
|
|
|
g.attrs[ATTRIBUTE_ ## name].fg = fg_; \
|
|
|
|
g.attrs[ATTRIBUTE_ ## name].bg = bg_; \
|
|
|
|
g.attrs[ATTRIBUTE_ ## name].attrs = attrs_;
|
2016-09-28 22:44:59 +02:00
|
|
|
ATTRIBUTE_TABLE (XX)
|
|
|
|
#undef XX
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_init_context (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_init (&g.poller);
|
|
|
|
mpd_client_init (&g.client, &g.poller);
|
|
|
|
config_init (&g.config);
|
|
|
|
strv_init (&g.streams);
|
|
|
|
item_list_init (&g.playlist);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_init (&g.playback_info);
|
|
|
|
g.playback_info.key_xfrm = tolower_ascii_strxfrm;
|
|
|
|
g.playback_info.free = free;
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// This is also approximately what libunistring does internally,
|
|
|
|
// since the locale name is canonicalized by locale_charset().
|
|
|
|
// Note that non-Unicode locales are handled pretty inefficiently.
|
2017-01-29 15:26:55 +01:00
|
|
|
g.locale_is_utf8 = !strcasecmp_ascii (locale_charset (), "UTF-8");
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-03 06:01:28 +02:00
|
|
|
// It doesn't work 100% (e.g. incompatible with undelining in urxvt)
|
|
|
|
// TODO: make this configurable
|
2017-01-29 15:26:55 +01:00
|
|
|
g.use_partial_boxes = g.locale_is_utf8;
|
2016-10-03 06:01:28 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
app_init_attributes ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_init_terminal (void)
|
|
|
|
{
|
|
|
|
TERMO_CHECK_VERSION;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!(g.tk = termo_new (STDIN_FILENO, NULL, 0)))
|
2016-09-28 22:44:59 +02:00
|
|
|
abort ();
|
|
|
|
if (!initscr () || nonl () == ERR)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
// Disable cursor, we're not going to use it most of the time
|
|
|
|
curs_set (0);
|
|
|
|
|
|
|
|
// By default we don't use any colors so they're not required...
|
|
|
|
if (start_color () == ERR
|
|
|
|
|| use_default_colors () == ERR
|
|
|
|
|| COLOR_PAIRS <= ATTRIBUTE_COUNT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int a = 0; a < ATTRIBUTE_COUNT; a++)
|
|
|
|
{
|
|
|
|
// ...thus we can reset back to defaults even after initializing some
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.attrs[a].fg >= COLORS || g.attrs[a].fg < -1
|
|
|
|
|| g.attrs[a].bg >= COLORS || g.attrs[a].bg < -1)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
app_init_attributes ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
init_pair (a + 1, g.attrs[a].fg, g.attrs[a].bg);
|
|
|
|
g.attrs[a].attrs |= COLOR_PAIR (a + 1);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_free_context (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
mpd_client_free (&g.client);
|
|
|
|
str_map_free (&g.playback_info);
|
|
|
|
strv_free (&g.streams);
|
|
|
|
item_list_free (&g.playlist);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
config_free (&g.config);
|
|
|
|
poller_free (&g.poller);
|
2017-06-04 23:58:04 +02:00
|
|
|
free (g.message);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.tk)
|
|
|
|
termo_destroy (g.tk);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_quit (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
g.quitting = true;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 20:14:49 +02:00
|
|
|
// So far there's nothing for us to wait on, so let's just stop looping;
|
|
|
|
// otherwise we might want to e.g. cleanly bring down the MPD interface
|
2017-01-29 15:26:55 +01:00
|
|
|
g.polling = false;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
app_is_character_in_locale (ucs4_t ch)
|
|
|
|
{
|
|
|
|
// Avoid the overhead joined with calling iconv() for all characters.
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.locale_is_utf8)
|
2016-09-28 22:44:59 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// The library really creates a new conversion object every single time
|
|
|
|
// and doesn't provide any smarter APIs. Luckily, most users use UTF-8.
|
|
|
|
size_t len;
|
|
|
|
char *tmp = u32_conv_to_encoding (locale_charset (), iconveh_error,
|
|
|
|
&ch, 1, NULL, NULL, &len);
|
|
|
|
if (!tmp)
|
|
|
|
return false;
|
|
|
|
free (tmp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
// --- Rendering ---------------------------------------------------------------
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-04 23:39:32 +02:00
|
|
|
// TODO: rewrite this so that it's fine-grained but not complicated
|
|
|
|
static void
|
|
|
|
app_invalidate (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_idle_set (&g.refresh_event);
|
2016-10-04 23:39:32 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 22:15:46 +02:00
|
|
|
static void
|
|
|
|
app_flush_buffer (struct row_buffer *buf, int width, chtype attrs)
|
|
|
|
{
|
|
|
|
row_buffer_align (buf, width, attrs);
|
|
|
|
row_buffer_flush (buf);
|
|
|
|
row_buffer_free (buf);
|
|
|
|
}
|
2016-10-04 23:39:32 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
/// Write the given UTF-8 string padded with spaces.
|
2016-10-04 22:21:30 +02:00
|
|
|
/// @param[in] attrs Text attributes for the text, including padding.
|
|
|
|
static void
|
|
|
|
app_write_line (const char *str, chtype attrs)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
|
|
|
row_buffer_append (&buf, str, attrs);
|
2016-10-07 22:15:46 +02:00
|
|
|
app_flush_buffer (&buf, COLS, attrs);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 22:15:46 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
static void
|
2016-10-07 22:15:46 +02:00
|
|
|
app_flush_header (struct row_buffer *buf, chtype attrs)
|
2016-10-01 23:43:51 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height++, 0);
|
2016-10-07 22:15:46 +02:00
|
|
|
app_flush_buffer (buf, COLS, attrs);
|
2016-10-01 23:43:51 +02:00
|
|
|
}
|
|
|
|
|
2016-10-01 21:20:07 +02:00
|
|
|
static void
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_song_info (void)
|
2016-10-01 21:20:07 +02:00
|
|
|
{
|
2016-10-12 13:11:17 +02:00
|
|
|
compact_map_t map;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!(map = item_list_get (&g.playlist, g.song)))
|
2016-10-01 21:20:07 +02:00
|
|
|
return;
|
|
|
|
|
2017-01-29 17:55:50 +01:00
|
|
|
chtype attr_normal = APP_ATTR (NORMAL);
|
2017-01-29 15:17:35 +01:00
|
|
|
chtype attr_highlight = APP_ATTR (HIGHLIGHT);
|
2016-10-02 01:15:52 +02:00
|
|
|
|
2016-10-01 21:20:07 +02:00
|
|
|
char *title;
|
2016-10-12 13:11:17 +02:00
|
|
|
if ((title = compact_map_find (map, "title"))
|
|
|
|
|| (title = compact_map_find (map, "name"))
|
|
|
|
|| (title = compact_map_find (map, "file")))
|
2016-10-01 21:20:07 +02:00
|
|
|
{
|
2016-10-01 23:43:51 +02:00
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
2017-01-29 15:17:35 +01:00
|
|
|
row_buffer_append (&buf, title, attr_highlight);
|
2017-01-29 17:55:50 +01:00
|
|
|
app_flush_header (&buf, attr_normal);
|
2016-10-01 21:20:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 13:11:17 +02:00
|
|
|
char *artist = compact_map_find (map, "artist");
|
|
|
|
char *album = compact_map_find (map, "album");
|
2016-10-02 01:15:52 +02:00
|
|
|
if (!artist && !album)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
|
|
|
|
|
|
|
if (artist)
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append_args (&buf, " by " + !buf.total_width, attr_normal,
|
2017-01-29 15:17:35 +01:00
|
|
|
artist, attr_highlight, NULL);
|
2016-10-02 01:15:52 +02:00
|
|
|
if (album)
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append_args (&buf, " from " + !buf.total_width, attr_normal,
|
2017-01-29 15:17:35 +01:00
|
|
|
album, attr_highlight, NULL);
|
2017-01-29 17:55:50 +01:00
|
|
|
app_flush_header (&buf, attr_normal);
|
2016-10-02 01:15:52 +02:00
|
|
|
}
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-23 01:01:07 +02:00
|
|
|
static char *
|
|
|
|
app_time_string (int seconds)
|
2016-10-02 01:15:52 +02:00
|
|
|
{
|
|
|
|
int minutes = seconds / 60; seconds %= 60;
|
2016-10-04 06:17:27 +02:00
|
|
|
int hours = minutes / 60; minutes %= 60;
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
struct str s;
|
|
|
|
str_init (&s);
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
if (hours)
|
|
|
|
str_append_printf (&s, "%d:%02d:", hours, minutes);
|
|
|
|
else
|
|
|
|
str_append_printf (&s, "%d:", minutes);
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
str_append_printf (&s, "%02d", seconds);
|
2016-10-23 01:01:07 +02:00
|
|
|
return str_steal (&s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_write_time (struct row_buffer *buf, int seconds, chtype attrs)
|
|
|
|
{
|
|
|
|
char *s = app_time_string (seconds);
|
|
|
|
row_buffer_append (buf, s, attrs);
|
|
|
|
free (s);
|
2016-10-02 01:15:52 +02:00
|
|
|
}
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 03:17:07 +02:00
|
|
|
static void
|
|
|
|
app_write_gauge (struct row_buffer *buf, float ratio, int width)
|
|
|
|
{
|
2016-10-02 04:09:54 +02:00
|
|
|
if (ratio < 0) ratio = 0;
|
|
|
|
if (ratio > 1) ratio = 1;
|
|
|
|
|
2016-10-02 03:17:07 +02:00
|
|
|
// Always compute it in exactly eight times the resolution,
|
|
|
|
// because sometimes Unicode is even useful
|
|
|
|
int len_left = ratio * width * 8 + 0.5;
|
|
|
|
|
2016-10-03 06:01:28 +02:00
|
|
|
static const char *partials[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉" };
|
|
|
|
int remainder = len_left % 8;
|
|
|
|
len_left /= 8;
|
2016-10-02 03:17:07 +02:00
|
|
|
|
|
|
|
const char *partial = NULL;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.use_partial_boxes)
|
2016-10-02 03:17:07 +02:00
|
|
|
partial = partials[remainder];
|
2016-10-03 06:01:28 +02:00
|
|
|
else
|
|
|
|
len_left += remainder >= (int) 4;
|
2016-10-02 03:17:07 +02:00
|
|
|
|
|
|
|
int len_right = width - len_left;
|
2016-10-06 07:21:36 +02:00
|
|
|
row_buffer_space (buf, len_left, APP_ATTR (ELAPSED));
|
2016-10-02 03:17:07 +02:00
|
|
|
if (partial && len_right-- > 0)
|
|
|
|
row_buffer_append (buf, partial, APP_ATTR (REMAINS));
|
2016-10-06 07:21:36 +02:00
|
|
|
row_buffer_space (buf, len_right, APP_ATTR (REMAINS));
|
2016-10-02 03:17:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
static void
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_status (void)
|
2016-10-02 01:15:52 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.state != PLAYER_STOPPED)
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_song_info ();
|
2016-10-02 01:15:52 +02:00
|
|
|
|
2017-01-29 17:55:50 +01:00
|
|
|
chtype attr_normal = APP_ATTR (NORMAL);
|
2017-01-29 15:17:35 +01:00
|
|
|
chtype attr_highlight = APP_ATTR (HIGHLIGHT);
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
bool stopped = g.state == PLAYER_STOPPED;
|
2017-01-29 17:55:50 +01:00
|
|
|
chtype attr_song_action = stopped ? attr_normal : attr_highlight;
|
2017-01-23 23:32:30 +01:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
const char *toggle = g.state == PLAYER_PLAYING ? "||" : "|>";
|
2017-01-23 23:32:30 +01:00
|
|
|
row_buffer_append_args (&buf,
|
2017-01-29 17:55:50 +01:00
|
|
|
"<<", attr_song_action, " ", attr_normal,
|
|
|
|
toggle, attr_highlight, " ", attr_normal,
|
|
|
|
"[]", attr_song_action, " ", attr_normal,
|
|
|
|
">>", attr_song_action, " ", attr_normal,
|
2017-01-23 23:32:30 +01:00
|
|
|
NULL);
|
2016-10-01 21:20:07 +02:00
|
|
|
|
|
|
|
if (stopped)
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append (&buf, "Stopped", attr_normal);
|
2016-10-01 21:20:07 +02:00
|
|
|
else
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.song_elapsed >= 0)
|
2016-10-01 23:43:51 +02:00
|
|
|
{
|
2017-01-29 17:55:50 +01:00
|
|
|
app_write_time (&buf, g.song_elapsed, attr_normal);
|
|
|
|
row_buffer_append (&buf, " ", attr_normal);
|
2016-10-01 23:43:51 +02:00
|
|
|
}
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.song_duration >= 1)
|
2016-10-01 23:43:51 +02:00
|
|
|
{
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append (&buf, "/ ", attr_normal);
|
|
|
|
app_write_time (&buf, g.song_duration, attr_normal);
|
|
|
|
row_buffer_append (&buf, " ", attr_normal);
|
2016-10-01 23:43:51 +02:00
|
|
|
}
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append (&buf, " ", attr_normal);
|
2016-10-02 01:15:52 +02:00
|
|
|
}
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
// It gets a bit complicated due to the only right-aligned item on the row
|
|
|
|
char *volume = NULL;
|
|
|
|
int remaining = COLS - buf.total_width;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.volume >= 0)
|
2016-10-02 01:15:52 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
volume = xstrdup_printf (" %3d%%", g.volume);
|
2016-10-02 01:15:52 +02:00
|
|
|
remaining -= strlen (volume);
|
|
|
|
}
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!stopped && g.song_elapsed >= 0 && g.song_duration >= 1
|
2016-10-02 01:15:52 +02:00
|
|
|
&& remaining > 0)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
g.gauge_offset = buf.total_width;
|
|
|
|
g.gauge_width = remaining;
|
2016-10-02 03:17:07 +02:00
|
|
|
app_write_gauge (&buf,
|
2017-01-29 15:26:55 +01:00
|
|
|
(float) g.song_elapsed / g.song_duration, remaining);
|
2016-10-01 21:20:07 +02:00
|
|
|
}
|
2016-10-06 07:21:36 +02:00
|
|
|
else
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_space (&buf, remaining, attr_normal);
|
2016-10-01 21:20:07 +02:00
|
|
|
|
2016-10-02 01:15:52 +02:00
|
|
|
if (volume)
|
|
|
|
{
|
2017-01-29 17:55:50 +01:00
|
|
|
row_buffer_append (&buf, volume, attr_normal);
|
2016-10-02 01:15:52 +02:00
|
|
|
free (volume);
|
|
|
|
}
|
2017-01-29 15:26:55 +01:00
|
|
|
g.controls_offset = g.header_height;
|
2017-01-29 17:55:50 +01:00
|
|
|
app_flush_header (&buf, attr_normal);
|
2016-10-01 21:20:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_header (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
g.header_height = 0;
|
2016-10-02 04:09:54 +02:00
|
|
|
|
2017-06-05 00:56:34 +02:00
|
|
|
g.tabs_offset = -1;
|
2017-01-29 15:26:55 +01:00
|
|
|
g.controls_offset = -1;
|
|
|
|
g.gauge_offset = -1;
|
|
|
|
g.gauge_width = 0;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
switch (g.client.state)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
case MPD_CONNECTED:
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_status ();
|
2016-09-28 22:44:59 +02:00
|
|
|
break;
|
|
|
|
case MPD_CONNECTING:
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height++, 0);
|
2017-01-29 17:55:50 +01:00
|
|
|
app_write_line ("Connecting to MPD...", APP_ATTR (NORMAL));
|
2016-09-28 22:44:59 +02:00
|
|
|
break;
|
|
|
|
case MPD_DISCONNECTED:
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height++, 0);
|
2017-01-29 17:55:50 +01:00
|
|
|
app_write_line ("Disconnected", APP_ATTR (NORMAL));
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 15:17:35 +01:00
|
|
|
chtype attrs[2] = { APP_ATTR (TAB_BAR), APP_ATTR (TAB_ACTIVE) };
|
2016-10-04 07:10:08 +02:00
|
|
|
|
2016-10-04 22:21:30 +02:00
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-04 22:21:30 +02:00
|
|
|
// The help tab is disguised so that it's not too intruding
|
2017-01-29 15:26:55 +01:00
|
|
|
row_buffer_append (&buf, APP_TITLE, attrs[g.active_tab == g.help_tab]);
|
2017-01-29 15:17:35 +01:00
|
|
|
row_buffer_append (&buf, " ", attrs[false]);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 00:56:34 +02:00
|
|
|
g.tabs_offset = g.header_height;
|
2017-01-29 15:26:55 +01:00
|
|
|
LIST_FOR_EACH (struct tab, iter, g.tabs)
|
|
|
|
row_buffer_append (&buf, iter->name, attrs[iter == g.active_tab]);
|
2017-01-29 15:17:35 +01:00
|
|
|
app_flush_header (&buf, attrs[false]);
|
2017-06-05 00:56:34 +02:00
|
|
|
|
|
|
|
const char *header = g.active_tab->header;
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
row_buffer_init (&buf);
|
|
|
|
row_buffer_append (&buf, header, APP_ATTR (HEADER));
|
|
|
|
app_flush_header (&buf, APP_ATTR (HEADER));
|
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 17:56:59 +01:00
|
|
|
static int
|
|
|
|
app_fitting_items (void)
|
|
|
|
{
|
|
|
|
// The raw number of items that would have fit on the terminal
|
2017-06-04 23:08:36 +02:00
|
|
|
return LINES - g.header_height - 1 /* status bar */;
|
2017-01-29 17:56:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 06:01:28 +02:00
|
|
|
static int
|
|
|
|
app_visible_items (void)
|
|
|
|
{
|
2017-01-29 17:56:59 +01:00
|
|
|
return MAX (0, app_fitting_items ());
|
2016-10-03 06:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_scrollbar (void)
|
2016-10-03 06:01:28 +02:00
|
|
|
{
|
|
|
|
// This assumes that we can write to the one-before-last column,
|
|
|
|
// i.e. that it's not covered by any double-wide character (and that
|
|
|
|
// ncurses comes to the right results when counting characters).
|
|
|
|
//
|
|
|
|
// We could also precompute the scrollbar and append it to each row
|
|
|
|
// as we render them, plus all the unoccupied rows.
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2016-10-03 06:01:28 +02:00
|
|
|
int visible_items = app_visible_items ();
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!g.use_partial_boxes)
|
2016-10-03 06:01:28 +02:00
|
|
|
{
|
|
|
|
// Apparently here we don't want the 0.5 rounding constant
|
|
|
|
int length = (float) visible_items / (int) tab->item_count
|
|
|
|
* (visible_items - 1);
|
|
|
|
int start = (float) tab->item_top / (int) tab->item_count
|
|
|
|
* (visible_items - 1);
|
|
|
|
|
|
|
|
for (int row = 0; row < visible_items; row++)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height + row, COLS - 1);
|
2016-10-03 06:01:28 +02:00
|
|
|
if (row < start || row > start + length + 1)
|
|
|
|
addch (' ' | APP_ATTR (SCROLLBAR));
|
|
|
|
else
|
|
|
|
addch (' ' | APP_ATTR (SCROLLBAR) | A_REVERSE);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: clamp the values, make sure they follow the right order
|
|
|
|
// We subtract half a character from both the top and the bottom, hence -1
|
2017-01-17 19:29:39 +01:00
|
|
|
// XXX: I'm not completely sure why we need that 0.5 constant in both
|
|
|
|
int length = (double) visible_items / tab->item_count
|
2016-10-03 06:01:28 +02:00
|
|
|
* (visible_items - 1) * 8 + 0.5;
|
2017-01-17 19:29:39 +01:00
|
|
|
int start = (double) tab->item_top / tab->item_count
|
2016-10-03 06:01:28 +02:00
|
|
|
* (visible_items - 1) * 8 + 0.5;
|
|
|
|
|
2017-01-17 19:29:39 +01:00
|
|
|
// Then we make sure the bar is at least one character high, hence +8
|
2016-10-03 06:01:28 +02:00
|
|
|
int end = start + length + 8;
|
|
|
|
|
|
|
|
int start_part = start % 8; start /= 8;
|
|
|
|
int end_part = end % 8; end /= 8;
|
|
|
|
|
|
|
|
// Even with this, the solid part must be at least one character high
|
|
|
|
static const char *partials[] = { "█", "▇", "▆", "▅", "▄", "▃", "▂", "▁" };
|
|
|
|
|
|
|
|
for (int row = 0; row < visible_items; row++)
|
|
|
|
{
|
|
|
|
chtype attrs = APP_ATTR (SCROLLBAR);
|
|
|
|
if (row > start && row <= end)
|
|
|
|
attrs ^= A_REVERSE;
|
|
|
|
|
|
|
|
const char *c = " ";
|
|
|
|
if (row == start) c = partials[start_part];
|
|
|
|
if (row == end) c = partials[end_part];
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height + row, COLS - 1);
|
2016-10-03 06:01:28 +02:00
|
|
|
|
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
|
|
|
row_buffer_append (&buf, c, attrs);
|
|
|
|
row_buffer_flush (&buf);
|
|
|
|
row_buffer_free (&buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_view (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height, 0);
|
2016-09-28 22:44:59 +02:00
|
|
|
clrtobot ();
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2016-10-03 06:01:28 +02:00
|
|
|
bool want_scrollbar = (int) tab->item_count > app_visible_items ();
|
|
|
|
int view_width = COLS - want_scrollbar;
|
|
|
|
|
2017-01-29 17:56:59 +01:00
|
|
|
int to_show =
|
|
|
|
MIN (app_fitting_items (), (int) tab->item_count - tab->item_top);
|
2016-10-03 06:01:28 +02:00
|
|
|
for (int row = 0; row < to_show; row++)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-03 06:01:28 +02:00
|
|
|
int item_index = tab->item_top + row;
|
2016-09-28 22:44:59 +02:00
|
|
|
int row_attrs = (item_index & 1) ? APP_ATTR (ODD) : APP_ATTR (EVEN);
|
2016-10-01 23:43:51 +02:00
|
|
|
if (item_index == tab->item_selected)
|
|
|
|
row_attrs = APP_ATTR (SELECTION);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
2016-10-05 05:58:19 +02:00
|
|
|
tab->on_item_draw (item_index, &buf, view_width);
|
2016-10-06 07:01:50 +02:00
|
|
|
|
|
|
|
// Combine attributes used by the handler with the defaults.
|
|
|
|
// Avoiding attrset() because of row_buffer_flush().
|
|
|
|
for (size_t i = 0; i < buf.chars_len; i++)
|
2016-10-03 19:43:51 +02:00
|
|
|
{
|
2016-10-06 07:01:50 +02:00
|
|
|
chtype *attrs = &buf.chars[i].attrs;
|
|
|
|
if (item_index == tab->item_selected)
|
|
|
|
*attrs = (*attrs & ~(A_COLOR | A_REVERSE)) | row_attrs;
|
|
|
|
else if ((*attrs & A_COLOR) && (row_attrs & A_COLOR))
|
|
|
|
*attrs |= (row_attrs & ~A_COLOR);
|
|
|
|
else
|
|
|
|
*attrs |= row_attrs;
|
2016-10-03 19:43:51 +02:00
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
move (g.header_height + row, 0);
|
2016-10-07 22:15:46 +02:00
|
|
|
app_flush_buffer (&buf, view_width, row_attrs);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
2016-10-03 06:01:28 +02:00
|
|
|
|
|
|
|
if (want_scrollbar)
|
2016-10-04 23:39:32 +02:00
|
|
|
app_draw_scrollbar ();
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 18:50:50 +02:00
|
|
|
static void
|
|
|
|
app_write_mpd_status_playlist (struct row_buffer *buf)
|
|
|
|
{
|
|
|
|
struct str stats;
|
|
|
|
str_init (&stats);
|
|
|
|
|
|
|
|
if (g.playlist.len == 1)
|
|
|
|
str_append_printf (&stats, "1 song ");
|
|
|
|
else
|
|
|
|
str_append_printf (&stats, "%zu songs ", g.playlist.len);
|
|
|
|
|
|
|
|
int hours = g.playlist_time / 3600;
|
|
|
|
int minutes = g.playlist_time % 3600 / 60;
|
|
|
|
if (hours || minutes)
|
|
|
|
{
|
|
|
|
str_append_c (&stats, ' ');
|
|
|
|
|
|
|
|
if (hours == 1)
|
|
|
|
str_append_printf (&stats, " 1 hour");
|
|
|
|
else if (hours)
|
|
|
|
str_append_printf (&stats, " %d hours", hours);
|
|
|
|
|
|
|
|
if (minutes == 1)
|
|
|
|
str_append_printf (&stats, " 1 minute");
|
|
|
|
else if (minutes)
|
|
|
|
str_append_printf (&stats, " %d minutes", minutes);
|
|
|
|
}
|
|
|
|
row_buffer_append (buf, stats.str, APP_ATTR (NORMAL));
|
|
|
|
str_free (&stats);
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:08:36 +02:00
|
|
|
static void
|
|
|
|
app_write_mpd_status (struct row_buffer *buf)
|
|
|
|
{
|
|
|
|
struct str_map *map = &g.playback_info;
|
2017-06-05 00:18:53 +02:00
|
|
|
if (str_map_find (map, "updating_db"))
|
2017-06-05 18:50:50 +02:00
|
|
|
row_buffer_append (buf, "Updating database...", APP_ATTR (NORMAL));
|
2017-06-04 23:08:36 +02:00
|
|
|
else
|
2017-06-05 18:50:50 +02:00
|
|
|
app_write_mpd_status_playlist (buf);
|
2017-06-04 23:08:36 +02:00
|
|
|
|
|
|
|
struct row_buffer right;
|
|
|
|
row_buffer_init (&right);
|
|
|
|
|
|
|
|
const char *s;
|
|
|
|
bool repeat = (s = str_map_find (map, "repeat")) && strcmp (s, "0");
|
|
|
|
bool random = (s = str_map_find (map, "random")) && strcmp (s, "0");
|
|
|
|
bool single = (s = str_map_find (map, "single")) && strcmp (s, "0");
|
|
|
|
bool consume = (s = str_map_find (map, "consume")) && strcmp (s, "0");
|
|
|
|
|
2017-06-05 01:09:55 +02:00
|
|
|
// TODO: remove the conditionals once we make them clickable
|
2017-06-04 23:08:36 +02:00
|
|
|
chtype a[2] = { APP_ATTR (NORMAL), APP_ATTR (HIGHLIGHT) };
|
2017-06-05 01:09:55 +02:00
|
|
|
if (repeat) row_buffer_append_args (&right,
|
2017-06-04 23:08:36 +02:00
|
|
|
" ", APP_ATTR (NORMAL), "repeat", a[repeat], NULL);
|
2017-06-05 01:09:55 +02:00
|
|
|
if (random) row_buffer_append_args (&right,
|
2017-06-04 23:08:36 +02:00
|
|
|
" ", APP_ATTR (NORMAL), "random", a[random], NULL);
|
2017-06-05 01:09:55 +02:00
|
|
|
if (single) row_buffer_append_args (&right,
|
2017-06-04 23:08:36 +02:00
|
|
|
" ", APP_ATTR (NORMAL), "single", a[single], NULL);
|
2017-06-05 01:09:55 +02:00
|
|
|
if (consume) row_buffer_append_args (&right,
|
2017-06-04 23:08:36 +02:00
|
|
|
" ", APP_ATTR (NORMAL), "consume", a[consume], NULL);
|
|
|
|
|
|
|
|
row_buffer_space (buf,
|
|
|
|
MAX (0, COLS - buf->total_width - right.total_width),
|
|
|
|
APP_ATTR (NORMAL));
|
|
|
|
row_buffer_append_buffer (buf, &right);
|
|
|
|
row_buffer_free (&right);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_draw_statusbar (void)
|
|
|
|
{
|
|
|
|
struct row_buffer buf;
|
|
|
|
row_buffer_init (&buf);
|
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
if (g.message)
|
|
|
|
row_buffer_append (&buf, g.message, APP_ATTR (HIGHLIGHT));
|
|
|
|
else if (g.client.state == MPD_CONNECTED)
|
2017-06-04 23:08:36 +02:00
|
|
|
app_write_mpd_status (&buf);
|
|
|
|
|
|
|
|
move (LINES - 1, 0);
|
|
|
|
app_flush_buffer (&buf, COLS, APP_ATTR (NORMAL));
|
|
|
|
}
|
|
|
|
|
2017-06-05 19:39:06 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 19:39:06 +02:00
|
|
|
/// Checks what items are visible and returns if the range was alright
|
2016-09-28 22:44:59 +02:00
|
|
|
static bool
|
2016-10-03 04:00:56 +02:00
|
|
|
app_fix_view_range (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2016-10-03 04:00:56 +02:00
|
|
|
if (tab->item_top < 0)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
tab->item_top = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:00:56 +02:00
|
|
|
// If the contents are at least as long as the screen, always fill it
|
|
|
|
int max_item_top = (int) tab->item_count - app_visible_items ();
|
|
|
|
// But don't let that suggest a negative offset
|
|
|
|
max_item_top = MAX (max_item_top, 0);
|
|
|
|
|
|
|
|
if (tab->item_top > max_item_top)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-03 04:00:56 +02:00
|
|
|
tab->item_top = max_item_top;
|
2016-09-28 22:44:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-05 19:39:06 +02:00
|
|
|
static void
|
|
|
|
app_on_refresh (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
poller_idle_reset (&g.refresh_event);
|
|
|
|
|
|
|
|
app_draw_header ();
|
|
|
|
app_fix_view_range();
|
|
|
|
app_draw_view ();
|
|
|
|
app_draw_statusbar ();
|
|
|
|
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Actions -----------------------------------------------------------------
|
|
|
|
|
2016-10-05 01:49:06 +02:00
|
|
|
/// Scroll down (positive) or up (negative) @a n items
|
2016-09-28 22:44:59 +02:00
|
|
|
static bool
|
2016-10-03 04:00:56 +02:00
|
|
|
app_scroll (int n)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
g.active_tab->item_top += n;
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-10-03 04:00:56 +02:00
|
|
|
return app_fix_view_range ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_ensure_selection_visible (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2017-06-04 00:05:20 +02:00
|
|
|
if (tab->item_selected < 0 || !tab->item_count)
|
2016-10-03 04:00:56 +02:00
|
|
|
return;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-03 04:00:56 +02:00
|
|
|
int too_high = tab->item_top - tab->item_selected;
|
|
|
|
if (too_high > 0)
|
|
|
|
app_scroll (-too_high);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-03 04:00:56 +02:00
|
|
|
int too_low = tab->item_selected
|
|
|
|
- (tab->item_top + app_visible_items () - 1);
|
|
|
|
if (too_low > 0)
|
|
|
|
app_scroll (too_low);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-10-03 04:00:56 +02:00
|
|
|
app_move_selection (int diff)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2017-06-05 19:39:06 +02:00
|
|
|
int fixed = tab->item_selected + diff;
|
2016-10-03 04:00:56 +02:00
|
|
|
fixed = MIN (fixed, (int) tab->item_count - 1);
|
2017-06-04 00:05:20 +02:00
|
|
|
fixed = MAX (fixed, 0);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 19:39:06 +02:00
|
|
|
bool result = !diff || tab->item_selected != fixed;
|
2016-10-03 04:00:56 +02:00
|
|
|
tab->item_selected = fixed;
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-03 04:00:56 +02:00
|
|
|
app_ensure_selection_visible ();
|
|
|
|
return result;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:49:06 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2016-10-10 11:13:52 +02:00
|
|
|
static void
|
|
|
|
app_prepend_tab (struct tab *tab)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
LIST_PREPEND (g.tabs, tab);
|
2016-10-10 11:13:52 +02:00
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:39:32 +02:00
|
|
|
static void
|
|
|
|
app_switch_tab (struct tab *tab)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
if (tab == g.active_tab)
|
2016-10-10 17:07:58 +02:00
|
|
|
return;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
g.last_tab = g.active_tab;
|
|
|
|
g.active_tab = tab;
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static bool
|
2016-10-02 06:34:00 +02:00
|
|
|
app_goto_tab (int tab_index)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-02 06:34:00 +02:00
|
|
|
int i = 0;
|
2017-01-29 15:26:55 +01:00
|
|
|
LIST_FOR_EACH (struct tab, iter, g.tabs)
|
2016-10-02 06:34:00 +02:00
|
|
|
if (i++ == tab_index)
|
|
|
|
{
|
2016-10-04 23:39:32 +02:00
|
|
|
app_switch_tab (iter);
|
2016-10-02 06:34:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- User input handling -----------------------------------------------------
|
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
#define ACTIONS(XX) \
|
2016-10-04 06:43:24 +02:00
|
|
|
XX( NONE, "Do nothing" ) \
|
|
|
|
\
|
|
|
|
XX( QUIT, "Quit application" ) \
|
|
|
|
XX( REDRAW, "Redraw screen" ) \
|
2016-10-10 17:07:58 +02:00
|
|
|
XX( HELP_TAB, "Switch to the help tab" ) \
|
|
|
|
XX( LAST_TAB, "Switch to previous tab" ) \
|
2016-10-04 06:43:24 +02:00
|
|
|
\
|
|
|
|
XX( MPD_TOGGLE, "Toggle play/pause" ) \
|
|
|
|
XX( MPD_STOP, "Stop playback" ) \
|
2017-06-05 20:02:21 +02:00
|
|
|
XX( MPD_PREVIOUS, "Previous song" ) \
|
2016-10-04 06:43:24 +02:00
|
|
|
XX( MPD_NEXT, "Next song" ) \
|
2017-06-05 19:57:00 +02:00
|
|
|
XX( MPD_BACKWARD, "Seek backwards" ) \
|
|
|
|
XX( MPD_FORWARD, "Seek forwards" ) \
|
2017-06-05 20:02:21 +02:00
|
|
|
XX( MPD_UPDATE_DB, "Update MPD database" ) \
|
2016-10-04 06:43:24 +02:00
|
|
|
XX( MPD_VOLUME_UP, "Increase volume" ) \
|
|
|
|
XX( MPD_VOLUME_DOWN, "Decrease volume" ) \
|
|
|
|
\
|
2016-10-05 06:44:13 +02:00
|
|
|
XX( MPD_ADD, "Add song to playlist" ) \
|
|
|
|
XX( MPD_REPLACE, "Replace playlist with song" ) \
|
|
|
|
\
|
|
|
|
XX( CHOOSE, "Choose item" ) \
|
2016-10-10 17:07:58 +02:00
|
|
|
XX( DELETE, "Delete item" ) \
|
2017-06-03 23:33:08 +02:00
|
|
|
XX( UP, "Go up a level" ) \
|
2016-10-05 06:44:13 +02:00
|
|
|
\
|
2016-10-04 06:43:24 +02:00
|
|
|
XX( SCROLL_UP, "Scroll up" ) \
|
|
|
|
XX( SCROLL_DOWN, "Scroll down" ) \
|
|
|
|
\
|
|
|
|
XX( GOTO_TOP, "Go to the top" ) \
|
|
|
|
XX( GOTO_BOTTOM, "Go to the bottom" ) \
|
|
|
|
XX( GOTO_ITEM_PREVIOUS, "Go to the previous item" ) \
|
|
|
|
XX( GOTO_ITEM_NEXT, "Go to the next item" ) \
|
|
|
|
XX( GOTO_PAGE_PREVIOUS, "Go to the previous page" ) \
|
2017-06-05 01:24:10 +02:00
|
|
|
XX( GOTO_PAGE_NEXT, "Go to the next page" ) \
|
|
|
|
\
|
|
|
|
XX( GOTO_VIEW_TOP, "Select the top item" ) \
|
|
|
|
XX( GOTO_VIEW_CENTER, "Select the center item" ) \
|
|
|
|
XX( GOTO_VIEW_BOTTOM, "Select the bottom item" )
|
2016-10-04 06:43:24 +02:00
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
enum action
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-12 16:06:23 +02:00
|
|
|
#define XX(name, description) ACTION_ ## name,
|
|
|
|
ACTIONS (XX)
|
2016-10-04 06:43:24 +02:00
|
|
|
#undef XX
|
2016-10-12 16:06:23 +02:00
|
|
|
ACTION_COUNT
|
2016-09-28 22:44:59 +02:00
|
|
|
};
|
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
static struct action_info
|
2016-10-04 06:43:24 +02:00
|
|
|
{
|
|
|
|
const char *name; ///< Name for user bindings
|
|
|
|
const char *description; ///< Human-readable description
|
|
|
|
}
|
2016-10-12 16:06:23 +02:00
|
|
|
g_actions[] =
|
2016-10-04 06:43:24 +02:00
|
|
|
{
|
|
|
|
#define XX(name, description) { #name, description },
|
2016-10-12 16:06:23 +02:00
|
|
|
ACTIONS (XX)
|
2016-10-04 06:43:24 +02:00
|
|
|
#undef XX
|
|
|
|
};
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2017-01-24 16:25:42 +01:00
|
|
|
static void
|
|
|
|
mpd_client_vsend_command (struct mpd_client *self, va_list ap)
|
|
|
|
{
|
|
|
|
struct strv v;
|
|
|
|
strv_init (&v);
|
|
|
|
|
|
|
|
const char *command;
|
|
|
|
while ((command = va_arg (ap, const char *)))
|
|
|
|
strv_append (&v, command);
|
|
|
|
mpd_client_send_commandv (self, v.vector);
|
|
|
|
strv_free (&v);
|
2016-10-02 04:09:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 16:25:42 +01:00
|
|
|
/// Send a command to MPD without caring about the response
|
2017-01-27 23:53:09 +01:00
|
|
|
static bool mpd_client_send_simple (struct mpd_client *self, ...)
|
2017-01-24 16:25:42 +01:00
|
|
|
ATTRIBUTE_SENTINEL;
|
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
static void
|
|
|
|
mpd_on_simple_response (const struct mpd_response *response,
|
|
|
|
const struct strv *data, void *user_data)
|
|
|
|
{
|
|
|
|
(void) data;
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
if (!response->success)
|
|
|
|
print_error ("%s: %s", "command failed", response->message_text);
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:25:42 +01:00
|
|
|
static bool
|
|
|
|
mpd_client_send_simple (struct mpd_client *self, ...)
|
|
|
|
{
|
|
|
|
if (self->state != MPD_CONNECTED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, self);
|
|
|
|
mpd_client_vsend_command (self, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
mpd_client_add_task (self, mpd_on_simple_response, NULL);
|
2017-01-24 16:25:42 +01:00
|
|
|
mpd_client_idle (self, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MPD_SIMPLE(...) \
|
2017-01-29 15:26:55 +01:00
|
|
|
mpd_client_send_simple (&g.client, __VA_ARGS__, NULL)
|
2017-01-24 16:25:42 +01:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static bool
|
2016-10-12 16:06:23 +02:00
|
|
|
app_process_action (enum action action)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-05 05:58:19 +02:00
|
|
|
// First let the tab try to handle this
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
2016-10-05 05:58:19 +02:00
|
|
|
if (tab->on_action && tab->on_action (action))
|
|
|
|
return true;
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
switch (action)
|
|
|
|
{
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_QUIT:
|
2016-10-07 00:36:43 +02:00
|
|
|
app_quit ();
|
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_REDRAW:
|
2016-09-28 22:44:59 +02:00
|
|
|
clear ();
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_LAST_TAB:
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!g.last_tab)
|
2016-10-10 17:07:58 +02:00
|
|
|
return false;
|
2017-01-29 15:26:55 +01:00
|
|
|
app_switch_tab (g.last_tab);
|
2016-10-10 17:07:58 +02:00
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_HELP_TAB:
|
2017-01-29 15:26:55 +01:00
|
|
|
app_switch_tab (g.help_tab);
|
2016-10-10 17:07:58 +02:00
|
|
|
break;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 00:18:53 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_TOGGLE:
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.state == PLAYER_PLAYING) MPD_SIMPLE ("pause", "1");
|
|
|
|
else if (g.state == PLAYER_PAUSED) MPD_SIMPLE ("pause", "0");
|
|
|
|
else MPD_SIMPLE ("play");
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2017-06-05 20:02:21 +02:00
|
|
|
case ACTION_MPD_STOP: MPD_SIMPLE ("stop"); break;
|
|
|
|
case ACTION_MPD_PREVIOUS: MPD_SIMPLE ("previous"); break;
|
|
|
|
case ACTION_MPD_NEXT: MPD_SIMPLE ("next"); break;
|
|
|
|
case ACTION_MPD_FORWARD: MPD_SIMPLE ("seekcur", "+10"); break;
|
|
|
|
case ACTION_MPD_BACKWARD: MPD_SIMPLE ("seekcur", "-10"); break;
|
|
|
|
case ACTION_MPD_UPDATE_DB: MPD_SIMPLE ("update"); break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_VOLUME_UP:
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.volume >= 0)
|
2016-10-02 05:47:33 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
char *volume = xstrdup_printf ("%d", MIN (100, g.volume + 10));
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("setvol", volume);
|
2016-10-02 05:47:33 +02:00
|
|
|
free (volume);
|
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_VOLUME_DOWN:
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.volume >= 0)
|
2016-10-02 05:47:33 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
char *volume = xstrdup_printf ("%d", MAX (0, g.volume - 10));
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("setvol", volume);
|
2016-10-02 05:47:33 +02:00
|
|
|
free (volume);
|
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-10-02 04:09:54 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2016-10-03 04:00:56 +02:00
|
|
|
// XXX: these should rather be parametrized
|
2017-06-05 20:02:21 +02:00
|
|
|
case ACTION_SCROLL_UP: app_scroll (-3); break;
|
|
|
|
case ACTION_SCROLL_DOWN: app_scroll (3); break;
|
2016-10-03 04:00:56 +02:00
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_TOP:
|
2016-10-03 04:00:56 +02:00
|
|
|
if (tab->item_count)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
g.active_tab->item_selected = 0;
|
2016-10-03 04:00:56 +02:00
|
|
|
app_ensure_selection_visible ();
|
2016-10-05 02:53:09 +02:00
|
|
|
app_invalidate ();
|
2016-10-03 04:00:56 +02:00
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_BOTTOM:
|
2016-10-03 04:00:56 +02:00
|
|
|
if (tab->item_count)
|
|
|
|
{
|
2017-06-04 00:05:20 +02:00
|
|
|
g.active_tab->item_selected =
|
|
|
|
MAX (0, (int) g.active_tab->item_count - 1);
|
2016-10-03 04:00:56 +02:00
|
|
|
app_ensure_selection_visible ();
|
2016-10-05 02:53:09 +02:00
|
|
|
app_invalidate ();
|
2016-10-03 04:00:56 +02:00
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-10-03 04:00:56 +02:00
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_ITEM_PREVIOUS:
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (-1);
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_ITEM_NEXT:
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (1);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_PAGE_PREVIOUS:
|
2017-06-05 00:56:34 +02:00
|
|
|
app_scroll (-app_visible_items ());
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (-app_visible_items ());
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_GOTO_PAGE_NEXT:
|
2017-06-05 00:56:34 +02:00
|
|
|
app_scroll (app_visible_items ());
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (app_visible_items ());
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-05 01:24:10 +02:00
|
|
|
case ACTION_GOTO_VIEW_TOP:
|
|
|
|
g.active_tab->item_selected = g.active_tab->item_top;
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (0);
|
2017-06-05 01:24:10 +02:00
|
|
|
case ACTION_GOTO_VIEW_CENTER:
|
|
|
|
g.active_tab->item_selected = g.active_tab->item_top;
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (MAX (0, app_visible_items () / 2 - 1));
|
2017-06-05 01:24:10 +02:00
|
|
|
case ACTION_GOTO_VIEW_BOTTOM:
|
|
|
|
g.active_tab->item_selected = g.active_tab->item_top;
|
2017-06-05 19:39:06 +02:00
|
|
|
return app_move_selection (MAX (0, app_visible_items () - 1));
|
2017-06-05 01:24:10 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_NONE:
|
2016-10-07 00:36:43 +02:00
|
|
|
break;
|
2016-09-28 22:44:59 +02:00
|
|
|
default:
|
2016-10-07 00:36:43 +02:00
|
|
|
return false;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2016-10-07 00:36:43 +02:00
|
|
|
static bool
|
2016-10-13 14:03:17 +02:00
|
|
|
app_process_left_mouse_click (int line, int column, bool double_click)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
if (line == g.controls_offset)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-02 04:09:54 +02:00
|
|
|
// XXX: there could be a push_widget(buf, text, attrs, handler)
|
|
|
|
// function to help with this but it might not be worth it
|
2016-10-12 16:06:23 +02:00
|
|
|
enum action action = ACTION_NONE;
|
|
|
|
if (column >= 0 && column <= 1) action = ACTION_MPD_PREVIOUS;
|
|
|
|
if (column >= 3 && column <= 4) action = ACTION_MPD_TOGGLE;
|
|
|
|
if (column >= 6 && column <= 7) action = ACTION_MPD_STOP;
|
|
|
|
if (column >= 9 && column <= 10) action = ACTION_MPD_NEXT;
|
2016-10-07 00:36:43 +02:00
|
|
|
|
2016-10-02 04:09:54 +02:00
|
|
|
if (action)
|
2016-10-12 16:06:23 +02:00
|
|
|
return app_process_action (action);
|
2016-10-02 04:09:54 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
int gauge_offset = column - g.gauge_offset;
|
|
|
|
if (g.gauge_offset < 0
|
|
|
|
|| gauge_offset < 0 || gauge_offset >= g.gauge_width)
|
2016-10-07 00:36:43 +02:00
|
|
|
return false;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
float position = (float) gauge_offset / g.gauge_width;
|
|
|
|
if (g.song_duration >= 1)
|
2016-10-02 04:09:54 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
char *where = xstrdup_printf ("%f", position * g.song_duration);
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("seekcur", where);
|
2016-10-07 00:36:43 +02:00
|
|
|
free (where);
|
2016-10-02 04:09:54 +02:00
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
2017-06-05 00:56:34 +02:00
|
|
|
else if (line == g.tabs_offset)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
struct tab *winner = NULL;
|
|
|
|
int indent = strlen (APP_TITLE);
|
|
|
|
if (column < indent)
|
2016-10-04 07:10:08 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
app_switch_tab (g.help_tab);
|
2016-10-07 00:36:43 +02:00
|
|
|
return true;
|
2016-10-04 07:10:08 +02:00
|
|
|
}
|
2017-01-29 15:26:55 +01:00
|
|
|
for (struct tab *iter = g.tabs; !winner && iter; iter = iter->next)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
if (column < (indent += iter->name_width))
|
|
|
|
winner = iter;
|
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
if (!winner)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
app_switch_tab (winner);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
2017-06-05 00:56:34 +02:00
|
|
|
else if (line >= g.header_height)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *tab = g.active_tab;
|
|
|
|
int row_index = line - g.header_height;
|
2016-10-02 05:12:13 +02:00
|
|
|
if (row_index < 0
|
|
|
|
|| row_index >= (int) tab->item_count - tab->item_top)
|
2016-10-07 00:36:43 +02:00
|
|
|
return false;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-03 08:26:17 +02:00
|
|
|
// TODO: handle the scrollbar a bit better than this
|
|
|
|
int visible_items = app_visible_items ();
|
|
|
|
if ((int) tab->item_count > visible_items && column == COLS - 1)
|
|
|
|
tab->item_top = (float) row_index / visible_items
|
|
|
|
* (int) tab->item_count - visible_items / 2;
|
|
|
|
else
|
|
|
|
tab->item_selected = row_index + tab->item_top;
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-10-13 14:03:17 +02:00
|
|
|
|
|
|
|
if (double_click)
|
|
|
|
app_process_action (ACTION_CHOOSE);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
2016-10-07 00:36:43 +02:00
|
|
|
return true;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-10-13 14:03:17 +02:00
|
|
|
app_process_mouse (termo_mouse_event_t type, int line, int column, int button,
|
|
|
|
bool double_click)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
if (type != TERMO_MOUSE_PRESS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (button == 1)
|
2016-10-13 14:03:17 +02:00
|
|
|
return app_process_left_mouse_click (line, column, double_click);
|
2016-09-28 22:44:59 +02:00
|
|
|
else if (button == 4)
|
2016-10-12 16:06:23 +02:00
|
|
|
return app_process_action (ACTION_SCROLL_UP);
|
2016-09-28 22:44:59 +02:00
|
|
|
else if (button == 5)
|
2016-10-12 16:06:23 +02:00
|
|
|
return app_process_action (ACTION_SCROLL_DOWN);
|
2016-10-07 00:36:43 +02:00
|
|
|
return false;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 04:53:11 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static struct binding
|
|
|
|
{
|
|
|
|
const char *key; ///< Key definition
|
2016-10-12 16:06:23 +02:00
|
|
|
enum action action; ///< Action to take
|
2017-01-24 16:23:20 +01:00
|
|
|
termo_key_t decoded; ///< Decoded key definition
|
2016-10-02 04:53:11 +02:00
|
|
|
}
|
|
|
|
g_default_bindings[] =
|
|
|
|
{
|
2017-01-24 16:23:20 +01:00
|
|
|
{ "Escape", ACTION_QUIT, {}},
|
|
|
|
{ "q", ACTION_QUIT, {}},
|
|
|
|
{ "C-l", ACTION_REDRAW, {}},
|
|
|
|
{ "M-Tab", ACTION_LAST_TAB, {}},
|
|
|
|
{ "F1", ACTION_HELP_TAB, {}},
|
|
|
|
|
|
|
|
{ "Home", ACTION_GOTO_TOP, {}},
|
|
|
|
{ "End", ACTION_GOTO_BOTTOM, {}},
|
|
|
|
{ "M-<", ACTION_GOTO_TOP, {}},
|
|
|
|
{ "M->", ACTION_GOTO_BOTTOM, {}},
|
|
|
|
{ "Up", ACTION_GOTO_ITEM_PREVIOUS, {}},
|
|
|
|
{ "Down", ACTION_GOTO_ITEM_NEXT, {}},
|
|
|
|
{ "k", ACTION_GOTO_ITEM_PREVIOUS, {}},
|
|
|
|
{ "j", ACTION_GOTO_ITEM_NEXT, {}},
|
|
|
|
{ "PageUp", ACTION_GOTO_PAGE_PREVIOUS, {}},
|
|
|
|
{ "PageDown", ACTION_GOTO_PAGE_NEXT, {}},
|
|
|
|
{ "C-p", ACTION_GOTO_ITEM_PREVIOUS, {}},
|
|
|
|
{ "C-n", ACTION_GOTO_ITEM_NEXT, {}},
|
|
|
|
{ "C-b", ACTION_GOTO_PAGE_PREVIOUS, {}},
|
|
|
|
{ "C-f", ACTION_GOTO_PAGE_NEXT, {}},
|
2016-10-02 05:47:33 +02:00
|
|
|
|
2017-06-05 01:24:10 +02:00
|
|
|
{ "H", ACTION_GOTO_VIEW_TOP, {}},
|
|
|
|
{ "M", ACTION_GOTO_VIEW_CENTER, {}},
|
|
|
|
{ "L", ACTION_GOTO_VIEW_BOTTOM, {}},
|
|
|
|
|
2016-10-02 05:47:33 +02:00
|
|
|
// Not sure how to set these up, they're pretty arbitrary so far
|
2017-01-24 16:23:20 +01:00
|
|
|
{ "Enter", ACTION_CHOOSE, {}},
|
|
|
|
{ "Delete", ACTION_DELETE, {}},
|
2017-06-03 23:33:08 +02:00
|
|
|
{ "Backspace", ACTION_UP, {}},
|
2017-01-24 16:23:20 +01:00
|
|
|
{ "a", ACTION_MPD_ADD, {}},
|
|
|
|
{ "r", ACTION_MPD_REPLACE, {}},
|
|
|
|
|
|
|
|
{ "Left", ACTION_MPD_PREVIOUS, {}},
|
|
|
|
{ "Right", ACTION_MPD_NEXT, {}},
|
2017-06-05 20:02:21 +02:00
|
|
|
{ "M-Left", ACTION_MPD_BACKWARD, {}},
|
|
|
|
{ "M-Right", ACTION_MPD_FORWARD, {}},
|
2017-01-24 16:23:20 +01:00
|
|
|
{ "h", ACTION_MPD_PREVIOUS, {}},
|
|
|
|
{ "l", ACTION_MPD_NEXT, {}},
|
|
|
|
{ "Space", ACTION_MPD_TOGGLE, {}},
|
|
|
|
{ "C-Space", ACTION_MPD_STOP, {}},
|
2017-06-05 20:02:21 +02:00
|
|
|
{ "u", ACTION_MPD_UPDATE_DB, {}},
|
2017-01-24 16:23:20 +01:00
|
|
|
{ "M-PageUp", ACTION_MPD_VOLUME_UP, {}},
|
|
|
|
{ "M-PageDown", ACTION_MPD_VOLUME_DOWN, {}},
|
2016-10-02 04:53:11 +02:00
|
|
|
};
|
|
|
|
|
2017-01-24 16:23:20 +01:00
|
|
|
static int
|
|
|
|
app_binding_cmp (const void *a, const void *b)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
return termo_keycmp (g.tk,
|
2017-01-24 16:23:20 +01:00
|
|
|
&((struct binding *) a)->decoded, &((struct binding *) b)->decoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_init_bindings (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-04 06:43:24 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (g_default_bindings); i++)
|
2016-10-02 04:53:11 +02:00
|
|
|
{
|
2016-10-04 06:43:24 +02:00
|
|
|
struct binding *binding = &g_default_bindings[i];
|
2017-01-29 15:26:55 +01:00
|
|
|
hard_assert (!*termo_strpkey_utf8 (g.tk,
|
2017-01-24 16:23:20 +01:00
|
|
|
binding->key, &binding->decoded, TERMO_FORMAT_ALTISMETA));
|
2016-10-02 04:53:11 +02:00
|
|
|
}
|
2017-01-24 16:23:20 +01:00
|
|
|
qsort (g_default_bindings, N_ELEMENTS (g_default_bindings),
|
|
|
|
sizeof *g_default_bindings, app_binding_cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
app_process_termo_event (termo_key_t *event)
|
|
|
|
{
|
|
|
|
struct binding dummy = { NULL, 0, *event }, *binding =
|
|
|
|
bsearch (&dummy, g_default_bindings, N_ELEMENTS (g_default_bindings),
|
|
|
|
sizeof *g_default_bindings, app_binding_cmp);
|
|
|
|
if (binding)
|
|
|
|
return app_process_action (binding->action);
|
2016-10-02 04:53:11 +02:00
|
|
|
|
|
|
|
// TODO: parametrize actions, put this among other bindings
|
2016-10-11 08:57:40 +02:00
|
|
|
if (!(event->modifiers & ~TERMO_KEYMOD_ALT)
|
2016-10-02 04:53:11 +02:00
|
|
|
&& event->code.codepoint >= '0'
|
|
|
|
&& event->code.codepoint <= '9')
|
|
|
|
{
|
|
|
|
int n = event->code.codepoint - '0';
|
2016-10-07 21:41:32 +02:00
|
|
|
if (app_goto_tab ((n == 0 ? 10 : n) - 1))
|
|
|
|
return true;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
2016-10-07 21:41:32 +02:00
|
|
|
return false;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 16:02:57 +02:00
|
|
|
// --- Current tab -------------------------------------------------------------
|
|
|
|
|
2017-06-04 04:31:33 +02:00
|
|
|
static struct tab g_current_tab;
|
2016-10-10 16:02:57 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
|
|
|
|
int width)
|
|
|
|
{
|
2016-10-23 01:01:07 +02:00
|
|
|
// TODO: configurable output, maybe dynamically sized columns
|
|
|
|
int length_len = 1 /*separator */ + 2 /* h */ + 3 /* m */+ 3 /* s */;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
compact_map_t map = item_list_get (&g.playlist, item_index);
|
2016-10-13 02:03:32 +02:00
|
|
|
const char *artist = compact_map_find (map, "artist");
|
|
|
|
const char *title = compact_map_find (map, "title");
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
chtype attrs = (int) item_index == g.song ? A_BOLD : 0;
|
2016-10-13 02:03:32 +02:00
|
|
|
if (artist && title)
|
2017-01-23 23:32:30 +01:00
|
|
|
row_buffer_append_args (buffer,
|
2016-10-13 02:03:32 +02:00
|
|
|
artist, attrs, " - ", attrs, title, attrs, NULL);
|
|
|
|
else
|
|
|
|
row_buffer_append (buffer, compact_map_find (map, "file"), attrs);
|
2016-10-23 01:01:07 +02:00
|
|
|
|
|
|
|
row_buffer_align (buffer, width - length_len, attrs);
|
|
|
|
|
|
|
|
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 *right_aligned = xstrdup_printf ("%*s", length_len, s);
|
|
|
|
row_buffer_append (buffer, right_aligned, attrs);
|
|
|
|
free (right_aligned);
|
|
|
|
free (s);
|
2016-10-10 16:02:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 17:07:58 +02:00
|
|
|
static bool
|
2016-10-12 16:06:23 +02:00
|
|
|
current_tab_on_action (enum action action)
|
2016-10-10 17:07:58 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *self = g.active_tab;
|
2017-06-04 00:05:20 +02:00
|
|
|
if (self->item_selected < 0 || !self->item_count)
|
2016-10-10 17:07:58 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
char *song;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_CHOOSE:
|
2016-10-10 17:07:58 +02:00
|
|
|
song = xstrdup_printf ("%d", self->item_selected);
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("play", song);
|
2016-10-10 17:07:58 +02:00
|
|
|
free (song);
|
|
|
|
return true;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_DELETE:
|
2016-10-10 17:07:58 +02:00
|
|
|
song = xstrdup_printf ("%d", self->item_selected);
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("delete", song);
|
2016-10-10 17:07:58 +02:00
|
|
|
free (song);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-10 16:02:57 +02:00
|
|
|
static void
|
|
|
|
current_tab_update (void)
|
|
|
|
{
|
2017-06-04 04:31:33 +02:00
|
|
|
g_current_tab.item_count = g.playlist.len;
|
2016-10-10 16:02:57 +02:00
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tab *
|
|
|
|
current_tab_init (void)
|
|
|
|
{
|
2017-06-04 04:31:33 +02:00
|
|
|
struct tab *super = &g_current_tab;
|
2016-10-10 16:02:57 +02:00
|
|
|
tab_init (super, "Current");
|
2016-10-10 17:07:58 +02:00
|
|
|
super->on_action = current_tab_on_action;
|
2016-10-10 16:02:57 +02:00
|
|
|
super->on_item_draw = current_tab_on_item_draw;
|
|
|
|
return super;
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:08:39 +02:00
|
|
|
// --- Library tab -------------------------------------------------------------
|
|
|
|
|
2017-06-04 04:17:46 +02:00
|
|
|
struct library_level
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct library_level)
|
|
|
|
|
|
|
|
int item_top; ///< Stored state
|
|
|
|
int item_selected; ///< Stored state
|
|
|
|
char path[]; ///< Path of the level
|
|
|
|
};
|
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
struct tab super; ///< Parent class
|
|
|
|
struct str path; ///< Current path
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv items; ///< Current items (type, name, path)
|
2017-06-04 04:17:46 +02:00
|
|
|
struct library_level *above; ///< Upper levels
|
2016-10-12 23:42:57 +02:00
|
|
|
}
|
|
|
|
g_library_tab;
|
|
|
|
|
2016-10-12 15:08:39 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
struct library_tab_item
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-10-12 23:42:57 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_append_owned (&g_library_tab.items,
|
2016-10-12 23:42:57 +02:00
|
|
|
xstrdup_printf ("%c%s%c%s", type, name, 0, path));
|
2016-10-12 15:08:39 +02:00
|
|
|
}
|
2016-10-12 23:42:57 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2016-10-12 15:08:39 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
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);
|
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
struct library_tab_item x =
|
|
|
|
library_tab_resolve (g_library_tab.items.vector[item_index]);
|
2017-01-23 23:32:30 +01:00
|
|
|
const char *prefix, *name;
|
2016-10-12 23:42:57 +02:00
|
|
|
switch (x.type)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2017-01-23 23:32:30 +01:00
|
|
|
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;
|
2016-10-12 15:08:39 +02:00
|
|
|
default: hard_assert (!"invalid item type");
|
|
|
|
}
|
2017-06-04 02:51:49 +02:00
|
|
|
chtype attrs = x.type != LIBRARY_FILE ? APP_ATTR (DIRECTORY) : 0;
|
|
|
|
row_buffer_append_args (buffer, prefix, attrs, name, attrs, NULL);
|
2016-10-12 15:08:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 00:11:30 +02:00
|
|
|
static char
|
|
|
|
library_tab_header_type (const char *key)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-10-13 00:11:30 +02:00
|
|
|
if (!strcasecmp_ascii (key, "file")) return LIBRARY_FILE;
|
|
|
|
if (!strcasecmp_ascii (key, "directory")) return LIBRARY_DIR;
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-12 15:08:39 +02:00
|
|
|
|
2016-10-13 00:11:30 +02:00
|
|
|
static void
|
|
|
|
library_tab_chunk (char type, const char *path, struct str_map *map)
|
|
|
|
{
|
2016-10-12 23:42:57 +02:00
|
|
|
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)
|
2016-10-13 00:11:30 +02:00
|
|
|
: xstrdup (xbasename (path));
|
|
|
|
library_tab_add (type, name, path);
|
2016-10-12 15:08:39 +02:00
|
|
|
free (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
library_tab_compare (char **a, char **b)
|
|
|
|
{
|
2016-10-12 23:42:57 +02:00
|
|
|
struct library_tab_item xa = library_tab_resolve (*a);
|
|
|
|
struct library_tab_item xb = library_tab_resolve (*b);
|
2016-10-12 15:08:39 +02:00
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
if (xa.type != xb.type)
|
|
|
|
return xa.type - xb.type;
|
2016-10-12 15:08:39 +02:00
|
|
|
|
2016-10-23 18:35:15 +02:00
|
|
|
return app_casecmp ((uint8_t *) xa.path, (uint8_t *) xb.path);
|
2016-10-12 15:08:39 +02:00
|
|
|
}
|
|
|
|
|
2017-06-03 23:42:18 +02:00
|
|
|
static char *
|
2017-06-04 04:17:46 +02:00
|
|
|
library_tab_parent (void)
|
2017-06-03 23:42:18 +02:00
|
|
|
{
|
|
|
|
struct str *path = &g_library_tab.path;
|
|
|
|
if (!path->len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *last_slash;
|
|
|
|
if ((last_slash = strrchr (path->str, '/')))
|
|
|
|
return xstrndup (path->str, last_slash - path->str);
|
|
|
|
return xstrdup ("");
|
|
|
|
}
|
|
|
|
|
2017-06-04 04:17:46 +02:00
|
|
|
static bool
|
|
|
|
library_tab_is_above (const char *above, const char *path)
|
|
|
|
{
|
|
|
|
size_t above_len = strlen (above);
|
|
|
|
if (strncmp (above, path, above_len))
|
|
|
|
return false;
|
|
|
|
// The root is an empty string and is above anything other than itself
|
|
|
|
return path[above_len] == '/' || (*path && !*above);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
library_tab_change_level (const char *new_path)
|
|
|
|
{
|
|
|
|
struct str *path = &g_library_tab.path;
|
|
|
|
if (!strcmp (path->str, new_path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct library_level *above;
|
|
|
|
if (library_tab_is_above (path->str, new_path))
|
|
|
|
{
|
|
|
|
above = xcalloc (1, sizeof *above + path->len + 1);
|
|
|
|
above->item_top = g_library_tab.super.item_top;
|
|
|
|
above->item_selected = g_library_tab.super.item_selected;
|
|
|
|
memcpy (above->path, path->str, path->len);
|
|
|
|
LIST_PREPEND (g_library_tab.above, above);
|
|
|
|
|
|
|
|
// Select the ".." entry to reflect Norton Commander
|
|
|
|
g_library_tab.super.item_top = 0;
|
|
|
|
g_library_tab.super.item_selected = 1;
|
|
|
|
}
|
|
|
|
else while ((above = g_library_tab.above)
|
|
|
|
&& !library_tab_is_above (above->path, new_path))
|
|
|
|
{
|
|
|
|
if (!strcmp (above->path, new_path))
|
|
|
|
{
|
|
|
|
g_library_tab.super.item_top = above->item_top;
|
|
|
|
g_library_tab.super.item_selected = above->item_selected;
|
|
|
|
}
|
|
|
|
g_library_tab.above = above->next;
|
|
|
|
free (above);
|
|
|
|
}
|
|
|
|
|
|
|
|
str_reset (path);
|
|
|
|
str_append (path, new_path);
|
2017-06-05 00:56:34 +02:00
|
|
|
|
|
|
|
free (g_library_tab.super.header);
|
|
|
|
g_library_tab.super.header = NULL;
|
|
|
|
|
|
|
|
if (path->len)
|
|
|
|
g_library_tab.super.header = xstrdup_printf ("/%s", path->str);
|
2017-06-04 04:17:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 15:08:39 +02:00
|
|
|
static void
|
|
|
|
library_tab_on_data (const struct mpd_response *response,
|
2017-01-23 23:32:30 +01:00
|
|
|
const struct strv *data, void *user_data)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2017-06-04 04:17:46 +02:00
|
|
|
char *new_path = user_data;
|
2016-10-12 15:08:39 +02:00
|
|
|
if (!response->success)
|
2017-06-04 01:00:58 +02:00
|
|
|
{
|
2017-06-04 23:58:04 +02:00
|
|
|
print_error ("cannot read directory: %s", response->message_text);
|
2017-06-04 04:17:46 +02:00
|
|
|
free (new_path);
|
2016-10-12 15:08:39 +02:00
|
|
|
return;
|
2017-06-04 01:00:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_reset (&g_library_tab.items);
|
2017-06-04 04:17:46 +02:00
|
|
|
library_tab_change_level (new_path);
|
|
|
|
free (new_path);
|
2016-10-12 23:42:57 +02:00
|
|
|
|
2017-06-04 04:17:46 +02:00
|
|
|
char *parent = library_tab_parent ();
|
|
|
|
if (parent)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
|
|
|
library_tab_add (LIBRARY_ROOT, "", "");
|
2017-06-04 04:17:46 +02:00
|
|
|
library_tab_add (LIBRARY_UP, "", parent);
|
|
|
|
free (parent);
|
2016-10-12 15:08:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct str_map map;
|
|
|
|
str_map_init (&map);
|
|
|
|
map.key_xfrm = tolower_ascii_strxfrm;
|
|
|
|
|
2016-10-13 00:11:30 +02:00
|
|
|
char *key, *value, type;
|
|
|
|
for (size_t i = data->len; i--; )
|
2016-10-13 01:48:45 +02:00
|
|
|
if (!(key = mpd_parse_kv (data->vector[i], &value)))
|
|
|
|
continue;
|
2016-10-13 00:11:30 +02:00
|
|
|
else if (!(type = library_tab_header_type (key)))
|
|
|
|
str_map_set (&map, key, value);
|
|
|
|
else
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-10-13 00:11:30 +02:00
|
|
|
library_tab_chunk (type, value, &map);
|
2016-10-12 15:08:39 +02:00
|
|
|
str_map_clear (&map);
|
|
|
|
}
|
|
|
|
str_map_free (&map);
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv *items = &g_library_tab.items;
|
2016-10-12 15:08:39 +02:00
|
|
|
qsort (items->vector, items->len, sizeof *items->vector,
|
|
|
|
(int (*) (const void *, const void *)) library_tab_compare);
|
2017-06-04 04:17:46 +02:00
|
|
|
g_library_tab.super.item_count = items->len;
|
2016-10-12 15:08:39 +02:00
|
|
|
|
2017-06-05 19:50:04 +02:00
|
|
|
// Don't force the selection visible when there's no need to touch it
|
|
|
|
if (g_library_tab.super.item_selected >= (int) items->len)
|
|
|
|
app_move_selection (0);
|
|
|
|
|
2016-10-12 15:08:39 +02:00
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
library_tab_reload (const char *new_path)
|
|
|
|
{
|
2017-06-04 01:00:58 +02:00
|
|
|
char *path = new_path
|
|
|
|
? xstrdup (new_path)
|
|
|
|
: xstrdup (g_library_tab.path.str);
|
2016-10-12 15:08:39 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2017-06-04 01:00:58 +02:00
|
|
|
mpd_client_send_command (c, "lsinfo", *path ? path : "/", NULL);
|
|
|
|
mpd_client_add_task (c, library_tab_on_data, path);
|
2016-10-12 15:08:39 +02:00
|
|
|
mpd_client_idle (c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-10-12 16:06:23 +02:00
|
|
|
library_tab_on_action (enum action action)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *self = g.active_tab;
|
2017-06-04 00:05:20 +02:00
|
|
|
if (self->item_selected < 0 || !self->item_count)
|
2016-10-12 15:08:39 +02:00
|
|
|
return false;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-10-12 15:08:39 +02:00
|
|
|
if (c->state != MPD_CONNECTED)
|
|
|
|
return false;
|
|
|
|
|
2016-10-12 23:42:57 +02:00
|
|
|
struct library_tab_item x =
|
|
|
|
library_tab_resolve (g_library_tab.items.vector[self->item_selected]);
|
2016-10-12 15:08:39 +02:00
|
|
|
switch (action)
|
|
|
|
{
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_CHOOSE:
|
2016-10-12 23:42:57 +02:00
|
|
|
switch (x.type)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-10-12 23:42:57 +02:00
|
|
|
case LIBRARY_ROOT:
|
2016-10-12 15:08:39 +02:00
|
|
|
case LIBRARY_UP:
|
2016-10-12 23:42:57 +02:00
|
|
|
case LIBRARY_DIR: library_tab_reload (x.path); break;
|
2017-01-24 16:25:42 +01:00
|
|
|
case LIBRARY_FILE: MPD_SIMPLE ("add", x.path); break;
|
2016-10-12 15:08:39 +02:00
|
|
|
default: hard_assert (!"invalid item type");
|
|
|
|
}
|
|
|
|
return true;
|
2017-06-03 23:33:08 +02:00
|
|
|
case ACTION_UP:
|
|
|
|
{
|
2017-06-04 04:17:46 +02:00
|
|
|
char *parent = library_tab_parent ();
|
|
|
|
if (parent)
|
2017-06-03 23:33:08 +02:00
|
|
|
{
|
2017-06-04 04:17:46 +02:00
|
|
|
library_tab_reload (parent);
|
|
|
|
free (parent);
|
2017-06-03 23:33:08 +02:00
|
|
|
}
|
2017-06-04 04:17:46 +02:00
|
|
|
return parent != NULL;
|
2017-06-03 23:33:08 +02:00
|
|
|
}
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_REPLACE:
|
2016-10-12 15:08:39 +02:00
|
|
|
// FIXME: we also need to play it if we've been playing things already
|
2016-10-12 23:42:57 +02:00
|
|
|
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
MPD_SIMPLE ("clear");
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("add", x.path);
|
2016-10-12 23:42:57 +02:00
|
|
|
return true;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_ADD:
|
2016-10-12 23:42:57 +02:00
|
|
|
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
|
|
|
|
break;
|
|
|
|
|
2017-01-24 16:25:42 +01:00
|
|
|
MPD_SIMPLE ("add", x.path);
|
2016-10-12 23:42:57 +02:00
|
|
|
return true;
|
2016-10-12 15:08:39 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tab *
|
|
|
|
library_tab_init (void)
|
|
|
|
{
|
|
|
|
str_init (&g_library_tab.path);
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_init (&g_library_tab.items);
|
2016-10-12 15:08:39 +02:00
|
|
|
|
|
|
|
struct tab *super = &g_library_tab.super;
|
|
|
|
tab_init (super, "Library");
|
|
|
|
super->on_action = library_tab_on_action;
|
|
|
|
super->on_item_draw = library_tab_on_item_draw;
|
|
|
|
return super;
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:54:40 +02:00
|
|
|
// --- Streams -----------------------------------------------------------------
|
|
|
|
|
2016-10-10 16:02:57 +02:00
|
|
|
// MPD can only parse m3u8 playlists, and only when it feels like doing so
|
|
|
|
|
2016-10-10 07:45:17 +02:00
|
|
|
struct stream_tab_task
|
2016-10-09 09:48:47 +02:00
|
|
|
{
|
2016-10-10 07:45:17 +02:00
|
|
|
struct poller_curl_task curl; ///< Superclass
|
2016-10-10 10:28:40 +02:00
|
|
|
struct str data; ///< Downloaded data
|
2016-10-10 07:45:17 +02:00
|
|
|
bool polling; ///< Still downloading
|
2016-10-10 10:28:40 +02:00
|
|
|
bool replace; ///< Should playlist be replaced?
|
2016-10-10 07:45:17 +02:00
|
|
|
};
|
2016-10-09 09:48:47 +02:00
|
|
|
|
|
|
|
static bool
|
|
|
|
is_content_type (const char *content_type,
|
|
|
|
const char *expected_type, const char *expected_subtype)
|
|
|
|
{
|
|
|
|
char *type = NULL, *subtype = NULL;
|
|
|
|
bool result = http_parse_media_type (content_type, &type, &subtype, NULL)
|
|
|
|
&& !strcasecmp_ascii (type, expected_type)
|
|
|
|
&& !strcasecmp_ascii (subtype, expected_subtype);
|
|
|
|
free (type);
|
|
|
|
free (subtype);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-10 10:28:40 +02:00
|
|
|
streams_tab_parse_playlist (const char *playlist, const char *content_type,
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv *out)
|
2016-10-09 09:48:47 +02:00
|
|
|
{
|
|
|
|
// We accept a lot of very broken stuff because this is the real world
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv lines;
|
|
|
|
strv_init (&lines);
|
2016-10-09 09:48:47 +02:00
|
|
|
cstr_split (playlist, "\r\n", true, &lines);
|
|
|
|
|
|
|
|
// Since this excludes '"', it should even work for XMLs (w/o entities)
|
|
|
|
const char *extract_re =
|
|
|
|
"(https?://([][a-z0-9._~:/?#@!$&'()*+,;=-]|%[a-f0-9]{2})+)";
|
|
|
|
if ((lines.len && !strcasecmp_ascii (lines.vector[0], "[playlist]"))
|
|
|
|
|| (content_type && is_content_type (content_type, "audio", "x-scpls")))
|
|
|
|
extract_re = "^File[^=]*=(.*)";
|
|
|
|
else if ((lines.len && !strcasecmp_ascii (lines.vector[0], "#EXTM3U"))
|
|
|
|
|| (content_type && is_content_type (content_type, "audio", "x-mpegurl")))
|
|
|
|
extract_re = "^([^#].*)";
|
|
|
|
|
|
|
|
regex_t *re = regex_compile (extract_re, REG_EXTENDED, NULL);
|
|
|
|
hard_assert (re != NULL);
|
|
|
|
|
|
|
|
regmatch_t groups[2];
|
|
|
|
for (size_t i = 0; i < lines.len; i++)
|
|
|
|
if (regexec (re, lines.vector[i], 2, groups, 0) != REG_NOMATCH)
|
2016-10-10 08:39:31 +02:00
|
|
|
{
|
|
|
|
char *target = xstrndup (lines.vector[i] + groups[1].rm_so,
|
|
|
|
groups[1].rm_eo - groups[1].rm_so);
|
|
|
|
if (utf8_validate (target, strlen (target)))
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_append_owned (out, target);
|
2016-10-10 08:39:31 +02:00
|
|
|
else
|
|
|
|
{
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_append_owned (out, latin1_to_utf8 (target));
|
2016-10-10 08:39:31 +02:00
|
|
|
free (target);
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:48:47 +02:00
|
|
|
regex_free (re);
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_free (&lines);
|
2016-10-09 09:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-10-10 10:28:40 +02:00
|
|
|
streams_tab_extract_links (struct str *data, const char *content_type,
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv *out)
|
2016-10-09 09:48:47 +02:00
|
|
|
{
|
2016-10-10 10:28:40 +02:00
|
|
|
// Since playlists are also "audio/*", this seems like a sane thing to do
|
|
|
|
for (size_t i = 0; i < data->len; i++)
|
|
|
|
{
|
|
|
|
uint8_t c = data->str[i];
|
|
|
|
if ((c < 32) & (c != '\t') & (c != '\r') & (c != '\n'))
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-09 09:48:47 +02:00
|
|
|
|
2016-10-10 10:28:40 +02:00
|
|
|
streams_tab_parse_playlist (data->str, content_type, out);
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-09 09:48:47 +02:00
|
|
|
|
2016-10-10 10:28:40 +02:00
|
|
|
static void
|
|
|
|
streams_tab_on_downloaded (CURLMsg *msg, struct poller_curl_task *task)
|
|
|
|
{
|
|
|
|
struct stream_tab_task *self =
|
|
|
|
CONTAINER_OF (task, struct stream_tab_task, curl);
|
|
|
|
self->polling = false;
|
|
|
|
|
|
|
|
if (msg->data.result
|
|
|
|
&& msg->data.result != CURLE_WRITE_ERROR)
|
2016-10-09 09:48:47 +02:00
|
|
|
{
|
2017-06-05 13:05:33 +02:00
|
|
|
cstr_uncapitalize (self->curl.curl_error);
|
2017-06-04 23:58:04 +02:00
|
|
|
print_error ("%s", self->curl.curl_error);
|
2016-10-10 10:28:40 +02:00
|
|
|
return;
|
2016-10-09 09:48:47 +02:00
|
|
|
}
|
2016-10-10 10:28:40 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-10-10 10:28:40 +02:00
|
|
|
if (c->state != MPD_CONNECTED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CURL *easy = msg->easy_handle;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
long code;
|
|
|
|
char *type, *uri;
|
|
|
|
if ((res = curl_easy_getinfo (easy, CURLINFO_RESPONSE_CODE, &code))
|
|
|
|
|| (res = curl_easy_getinfo (easy, CURLINFO_CONTENT_TYPE, &type))
|
|
|
|
|| (res = curl_easy_getinfo (easy, CURLINFO_EFFECTIVE_URL, &uri)))
|
|
|
|
{
|
|
|
|
print_error ("%s: %s",
|
|
|
|
"cURL info retrieval failed", curl_easy_strerror (res));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// cURL is not willing to parse the ICY header, the code is zero then
|
|
|
|
if (code && code != 200)
|
|
|
|
{
|
2017-06-04 23:58:04 +02:00
|
|
|
print_error ("%s: %ld", "unexpected HTTP response", code);
|
2016-10-10 10:28:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpd_client_list_begin (c);
|
|
|
|
|
|
|
|
// FIXME: we also need to play it if we've been playing things already
|
|
|
|
if (self->replace)
|
|
|
|
mpd_client_send_command (c, "clear", NULL);
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv links;
|
|
|
|
strv_init (&links);
|
2016-10-10 10:28:40 +02:00
|
|
|
|
|
|
|
if (!streams_tab_extract_links (&self->data, type, &links))
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_append (&links, uri);
|
2016-10-10 10:28:40 +02:00
|
|
|
for (size_t i = 0; i < links.len; i++)
|
|
|
|
mpd_client_send_command (c, "add", links.vector[i], NULL);
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_free (&links);
|
2016-10-10 10:28:40 +02:00
|
|
|
mpd_client_list_end (c);
|
|
|
|
mpd_client_add_task (c, NULL, NULL);
|
|
|
|
mpd_client_idle (c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
write_callback (char *ptr, size_t size, size_t nmemb, void *user_data)
|
|
|
|
{
|
|
|
|
struct str *buf = user_data;
|
|
|
|
str_append_data (buf, ptr, size * nmemb);
|
|
|
|
|
|
|
|
// Invoke CURLE_WRITE_ERROR when we've received enough data for a playlist
|
|
|
|
if (buf->len >= (1 << 16))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
streams_tab_process (const char *uri, bool replace, struct error **e)
|
|
|
|
{
|
|
|
|
struct poller poller;
|
|
|
|
poller_init (&poller);
|
|
|
|
|
|
|
|
struct poller_curl pc;
|
|
|
|
hard_assert (poller_curl_init (&pc, &poller, NULL));
|
|
|
|
struct stream_tab_task task;
|
|
|
|
hard_assert (poller_curl_spawn (&task.curl, NULL));
|
|
|
|
|
|
|
|
CURL *easy = task.curl.easy;
|
|
|
|
str_init (&task.data);
|
|
|
|
task.replace = replace;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
CURLcode res;
|
|
|
|
if ((res = curl_easy_setopt (easy, CURLOPT_FOLLOWLOCATION, 1L))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_NOPROGRESS, 1L))
|
|
|
|
// TODO: make the timeout a bit larger once we're asynchronous
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_TIMEOUT, 5L))
|
|
|
|
// Not checking anything, we just want some data, any data
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_SSL_VERIFYPEER, 0L))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_SSL_VERIFYHOST, 0L))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_URL, uri))
|
|
|
|
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_VERBOSE, (long) g_debug_mode))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_DEBUGFUNCTION, print_curl_debug))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_WRITEDATA, &task.data))
|
|
|
|
|| (res = curl_easy_setopt (easy, CURLOPT_WRITEFUNCTION, write_callback)))
|
|
|
|
{
|
|
|
|
error_set (e, "%s: %s", "cURL setup failed", curl_easy_strerror (res));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.curl.on_done = streams_tab_on_downloaded;
|
|
|
|
hard_assert (poller_curl_add (&pc, task.curl.easy, NULL));
|
|
|
|
|
|
|
|
// TODO: don't run a subloop, run the task fully asynchronously
|
|
|
|
task.polling = true;
|
|
|
|
while (task.polling)
|
|
|
|
poller_run (&poller);
|
|
|
|
|
|
|
|
hard_assert (poller_curl_remove (&pc, task.curl.easy, NULL));
|
|
|
|
result = true;
|
2016-10-09 09:48:47 +02:00
|
|
|
|
|
|
|
error:
|
2016-10-10 10:28:40 +02:00
|
|
|
curl_easy_cleanup (task.curl.easy);
|
|
|
|
str_free (&task.data);
|
|
|
|
poller_curl_free (&pc);
|
|
|
|
|
|
|
|
poller_free (&poller);
|
|
|
|
return result;
|
2016-10-09 09:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-10-12 16:06:23 +02:00
|
|
|
streams_tab_on_action (enum action action)
|
2016-10-09 09:48:47 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct tab *self = g.active_tab;
|
2017-06-04 00:05:20 +02:00
|
|
|
if (self->item_selected < 0 || !self->item_count)
|
2016-10-09 09:48:47 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// For simplicity the URL is the string following the stream name
|
2017-01-29 15:26:55 +01:00
|
|
|
const char *uri = 1 + strchr (g.streams.vector[self->item_selected], 0);
|
2016-10-09 09:48:47 +02:00
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
struct error *e = NULL;
|
2016-10-09 09:48:47 +02:00
|
|
|
switch (action)
|
|
|
|
{
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_MPD_REPLACE:
|
2017-06-04 23:58:04 +02:00
|
|
|
streams_tab_process (uri, true, &e);
|
|
|
|
break;
|
2016-10-12 16:06:23 +02:00
|
|
|
case ACTION_CHOOSE:
|
|
|
|
case ACTION_MPD_ADD:
|
2017-06-04 23:58:04 +02:00
|
|
|
streams_tab_process (uri, false, &e);
|
|
|
|
break;
|
2016-10-09 09:48:47 +02:00
|
|
|
default:
|
2016-10-10 10:28:40 +02:00
|
|
|
return false;
|
2016-10-09 09:48:47 +02:00
|
|
|
}
|
2017-06-04 23:58:04 +02:00
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
print_error ("%s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
}
|
|
|
|
return true;
|
2016-10-09 09:48:47 +02:00
|
|
|
}
|
2016-10-05 05:54:40 +02:00
|
|
|
|
|
|
|
static void
|
2016-10-05 05:58:19 +02:00
|
|
|
streams_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
|
|
|
|
int width)
|
2016-10-05 05:54:40 +02:00
|
|
|
{
|
|
|
|
(void) width;
|
2017-01-29 15:26:55 +01:00
|
|
|
row_buffer_append (buffer, g.streams.vector[item_index], 0);
|
2016-10-05 05:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tab *
|
|
|
|
streams_tab_init (void)
|
|
|
|
{
|
|
|
|
static struct tab super;
|
|
|
|
tab_init (&super, "Streams");
|
2016-10-09 09:48:47 +02:00
|
|
|
super.on_action = streams_tab_on_action;
|
2016-10-05 05:54:40 +02:00
|
|
|
super.on_item_draw = streams_tab_on_item_draw;
|
2017-01-29 15:26:55 +01:00
|
|
|
super.item_count = g.streams.len;
|
2016-10-05 05:54:40 +02:00
|
|
|
return &super;
|
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
// --- Info tab ----------------------------------------------------------------
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
struct tab super; ///< Parent class
|
2017-01-23 23:32:30 +01:00
|
|
|
struct strv keys; ///< Data keys
|
|
|
|
struct strv values; ///< Data values
|
2016-10-05 01:12:31 +02:00
|
|
|
}
|
|
|
|
g_info_tab;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
static void
|
2016-10-05 05:58:19 +02:00
|
|
|
info_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
(void) width;
|
|
|
|
|
2016-10-05 02:54:43 +02:00
|
|
|
// It looks like we could do with a generic list structure that just
|
|
|
|
// stores formatted row_buffers. Let's see for other tabs:
|
|
|
|
// - Current -- unusable, has dynamic column alignment
|
|
|
|
// - Library -- could work for the "icons"
|
|
|
|
// - Streams -- useless
|
|
|
|
// - Debug -- it'd take up considerably more space
|
|
|
|
// However so far we're only showing show key-value pairs.
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
row_buffer_append_args (buffer,
|
2016-10-05 02:54:43 +02:00
|
|
|
g_info_tab.keys.vector[item_index], A_BOLD, ":", A_BOLD, NULL);
|
2016-10-06 07:21:36 +02:00
|
|
|
row_buffer_space (buffer, 8 - buffer->total_width, 0);
|
2016-10-05 02:54:43 +02:00
|
|
|
row_buffer_append (buffer, g_info_tab.values.vector[item_index], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-12 13:11:17 +02:00
|
|
|
info_tab_add (compact_map_t data, const char *field)
|
2016-10-05 02:54:43 +02:00
|
|
|
{
|
2016-10-12 13:11:17 +02:00
|
|
|
const char *value = compact_map_find (data, field);
|
2016-10-05 02:54:43 +02:00
|
|
|
if (!value) value = "";
|
|
|
|
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_append (&g_info_tab.keys, field);
|
|
|
|
strv_append (&g_info_tab.values, value);
|
2016-10-05 02:54:43 +02:00
|
|
|
g_info_tab.super.item_count++;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:49:06 +02:00
|
|
|
static void
|
|
|
|
info_tab_update (void)
|
|
|
|
{
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_reset (&g_info_tab.keys);
|
|
|
|
strv_reset (&g_info_tab.values);
|
2016-10-05 02:54:43 +02:00
|
|
|
g_info_tab.super.item_count = 0;
|
|
|
|
|
2016-10-12 13:11:17 +02:00
|
|
|
compact_map_t map;
|
2017-01-29 15:26:55 +01:00
|
|
|
if ((map = item_list_get (&g.playlist, g.song)))
|
2016-10-10 16:02:57 +02:00
|
|
|
{
|
|
|
|
info_tab_add (map, "Title");
|
|
|
|
info_tab_add (map, "Artist");
|
|
|
|
info_tab_add (map, "Album");
|
|
|
|
info_tab_add (map, "Track");
|
|
|
|
info_tab_add (map, "Genre");
|
2016-10-10 16:30:15 +02:00
|
|
|
// Yes, it is "file", but this is also for display
|
|
|
|
info_tab_add (map, "File");
|
2016-10-10 16:02:57 +02:00
|
|
|
}
|
2016-10-05 01:49:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static struct tab *
|
2016-10-05 01:49:06 +02:00
|
|
|
info_tab_init (void)
|
2016-10-05 01:12:31 +02:00
|
|
|
{
|
2017-01-23 23:32:30 +01:00
|
|
|
strv_init (&g_info_tab.keys);
|
|
|
|
strv_init (&g_info_tab.values);
|
2016-10-05 02:54:43 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
struct tab *super = &g_info_tab.super;
|
|
|
|
tab_init (super, "Info");
|
|
|
|
super->on_item_draw = info_tab_on_item_draw;
|
|
|
|
return super;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Help tab ----------------------------------------------------------------
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
2016-10-05 05:58:19 +02:00
|
|
|
help_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
(void) width;
|
|
|
|
|
|
|
|
// TODO: group them the other way around for clarity
|
|
|
|
hard_assert (item_index < N_ELEMENTS (g_default_bindings));
|
|
|
|
struct binding *binding = &g_default_bindings[item_index];
|
|
|
|
char *text = xstrdup_printf ("%-12s %s",
|
2016-10-12 16:06:23 +02:00
|
|
|
binding->key, g_actions[binding->action].description);
|
2016-10-05 01:12:31 +02:00
|
|
|
row_buffer_append (buffer, text, 0);
|
|
|
|
free (text);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static struct tab *
|
2016-10-05 01:49:06 +02:00
|
|
|
help_tab_init (void)
|
2016-10-05 01:12:31 +02:00
|
|
|
{
|
2016-10-05 05:58:19 +02:00
|
|
|
static struct tab super;
|
|
|
|
tab_init (&super, "Help");
|
|
|
|
super.on_item_draw = help_tab_on_item_draw;
|
|
|
|
super.item_count = N_ELEMENTS (g_default_bindings);
|
|
|
|
return &super;
|
2016-10-05 01:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Debug tab ---------------------------------------------------------------
|
|
|
|
|
|
|
|
struct debug_item
|
|
|
|
{
|
|
|
|
char *text; ///< Logged line
|
|
|
|
int64_t timestamp; ///< Timestamp
|
|
|
|
chtype attrs; ///< Line attributes
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
struct tab super; ///< Parent class
|
2017-06-05 18:57:54 +02:00
|
|
|
ARRAY (struct debug_item, items) ///< Items
|
2016-10-05 01:12:31 +02:00
|
|
|
bool active; ///< The tab is present
|
|
|
|
}
|
|
|
|
g_debug_tab;
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
2016-10-05 05:58:19 +02:00
|
|
|
debug_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-06-05 18:57:54 +02:00
|
|
|
hard_assert (item_index < g_debug_tab.items_len);
|
2016-10-05 01:12:31 +02:00
|
|
|
struct debug_item *item = &g_debug_tab.items[item_index];
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
char buf[16];
|
|
|
|
struct tm tm;
|
|
|
|
time_t when = item->timestamp / 1000;
|
|
|
|
strftime (buf, sizeof buf, "%T", localtime_r (&when, &tm));
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
char *prefix = xstrdup_printf
|
|
|
|
("%s.%03d", buf, (int) (item->timestamp % 1000));
|
|
|
|
row_buffer_append (buffer, prefix, 0);
|
|
|
|
free (prefix);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
row_buffer_append (buffer, " ", item->attrs);
|
|
|
|
row_buffer_append (buffer, item->text, item->attrs);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
// We override the formatting including colors -- do it for the whole line
|
2016-10-07 22:15:46 +02:00
|
|
|
row_buffer_align (buffer, width, item->attrs);
|
2016-10-05 01:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-05 20:09:11 +02:00
|
|
|
debug_tab_push (char *message, chtype attrs)
|
2016-10-05 01:12:31 +02:00
|
|
|
{
|
2017-06-05 18:57:54 +02:00
|
|
|
ARRAY_RESERVE (g_debug_tab.items, 1);
|
|
|
|
struct debug_item *item = &g_debug_tab.items[g_debug_tab.items_len++];
|
|
|
|
g_debug_tab.super.item_count = g_debug_tab.items_len;
|
2017-06-05 20:09:11 +02:00
|
|
|
item->text = message;
|
2016-10-05 01:12:31 +02:00
|
|
|
item->attrs = attrs;
|
|
|
|
item->timestamp = clock_msec (CLOCK_REALTIME);
|
|
|
|
|
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tab *
|
2016-10-05 01:49:06 +02:00
|
|
|
debug_tab_init (void)
|
2016-10-05 01:12:31 +02:00
|
|
|
{
|
2017-06-05 18:57:54 +02:00
|
|
|
ARRAY_INIT (g_debug_tab.items);
|
2016-10-05 01:12:31 +02:00
|
|
|
g_debug_tab.active = true;
|
|
|
|
|
|
|
|
struct tab *super = &g_debug_tab.super;
|
|
|
|
tab_init (super, "Debug");
|
|
|
|
super->on_item_draw = debug_tab_on_item_draw;
|
|
|
|
return super;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- MPD interface -----------------------------------------------------------
|
|
|
|
|
2017-01-28 01:04:34 +01:00
|
|
|
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;
|
|
|
|
*optional_msec = MIN (INT_MAX, n);
|
|
|
|
}
|
|
|
|
unsigned long n = strtoul (value, &end, 10);
|
|
|
|
if (end == period || !*end)
|
|
|
|
*sec = MIN (INT_MAX, n);
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:50:50 +02:00
|
|
|
static void
|
|
|
|
mpd_update_playlist_time (void)
|
|
|
|
{
|
|
|
|
g.playlist_time = 0;
|
|
|
|
|
|
|
|
// It would also be possible to retrieve this from "stats" -> "playtime"
|
|
|
|
unsigned long n;
|
|
|
|
for (size_t i = 0; i < g.playlist.len; i++)
|
|
|
|
{
|
|
|
|
compact_map_t map = item_list_get (&g.playlist, i);
|
|
|
|
const char *time = compact_map_find (map, "time");
|
|
|
|
if (time && xstrtoul (&n, time, 10))
|
|
|
|
g.playlist_time += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
2016-10-05 01:49:06 +02:00
|
|
|
mpd_update_playback_state (void)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct str_map *map = &g.playback_info;
|
|
|
|
g.song_elapsed = g.song_duration = g.volume = g.song = -1;
|
2017-06-05 18:50:50 +02:00
|
|
|
uint32_t last_playlist_version = g.playlist_version;
|
2017-01-29 15:26:55 +01:00
|
|
|
g.playlist_version = 0;
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-05 01:49:06 +02:00
|
|
|
const char *state;
|
2017-01-29 18:21:27 +01:00
|
|
|
g.state = PLAYER_STOPPED;
|
2016-10-05 01:49:06 +02:00
|
|
|
if ((state = str_map_find (map, "state")))
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-01-29 18:21:27 +01:00
|
|
|
if (!strcmp (state, "play")) g.state = PLAYER_PLAYING;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (!strcmp (state, "pause")) g.state = PLAYER_PAUSED;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 01:04:34 +01:00
|
|
|
// Values in "time" are always rounded. "elapsed", introduced in MPD 0.16,
|
|
|
|
// is in millisecond precision and "duration" as well, starting with 0.20.
|
|
|
|
// Prefer the more precise values but use what we have.
|
|
|
|
const char *time = str_map_find (map, "time");
|
|
|
|
const char *elapsed = str_map_find (map, "elapsed");
|
|
|
|
const char *duration = str_map_find (map, "duration");
|
|
|
|
|
|
|
|
struct strv fields;
|
|
|
|
strv_init (&fields);
|
2016-10-01 21:20:07 +02:00
|
|
|
if (time)
|
|
|
|
{
|
2017-01-28 01:04:34 +01:00
|
|
|
cstr_split (time, ":", false, &fields);
|
|
|
|
if (fields.len >= 1 && !elapsed) elapsed = fields.vector[0];
|
|
|
|
if (fields.len >= 2 && !duration) duration = fields.vector[1];
|
2016-10-01 21:20:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 18:33:14 +02:00
|
|
|
int msec_past_second = 0;
|
2017-01-29 15:26:55 +01:00
|
|
|
mpd_read_time (elapsed, &g.song_elapsed, &msec_past_second);
|
|
|
|
mpd_read_time (duration, &g.song_duration, NULL);
|
2017-01-28 01:04:34 +01:00
|
|
|
strv_free (&fields);
|
2016-10-04 18:33:14 +02:00
|
|
|
|
2017-01-28 01:04:34 +01:00
|
|
|
// We could also just poll the server each half a second but let's not
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_reset (&g.elapsed_event);
|
|
|
|
if (g.state == PLAYER_PLAYING)
|
2016-10-05 00:13:15 +02:00
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_set (&g.elapsed_event, 1000 - msec_past_second);
|
|
|
|
g.elapsed_since = clock_msec (CLOCK_BEST) - msec_past_second;
|
2016-10-05 00:13:15 +02:00
|
|
|
}
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-04 18:33:14 +02:00
|
|
|
// The server sends -1 when nothing is being played right now
|
2017-01-28 01:04:34 +01:00
|
|
|
unsigned long n;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (xstrtoul_map (map, "volume", &n)) g.volume = n;
|
2016-10-10 16:02:57 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
if (xstrtoul_map (map, "playlist", &n)) g.playlist_version = n;
|
|
|
|
if (xstrtoul_map (map, "song", &n)) g.song = n;
|
2017-01-28 01:04:34 +01:00
|
|
|
|
2017-06-05 18:50:50 +02:00
|
|
|
if (g.playlist_version != last_playlist_version)
|
|
|
|
mpd_update_playlist_time ();
|
|
|
|
|
2017-01-28 01:04:34 +01:00
|
|
|
app_invalidate ();
|
2016-10-05 01:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
2017-01-23 23:32:30 +01:00
|
|
|
mpd_process_info (const struct strv *data)
|
2016-10-05 01:49:06 +02:00
|
|
|
{
|
2017-01-29 18:18:58 +01:00
|
|
|
struct str_map *map = &g.playback_info;
|
2016-10-05 01:49:06 +02:00
|
|
|
|
2016-10-13 01:41:28 +02:00
|
|
|
// First there's the status, followed by playlist items chunked by "file"
|
|
|
|
unsigned long n; char *key, *value;
|
|
|
|
for (size_t i = 0; i < data->len - 1 && data->vector[i]; i++)
|
2016-10-10 16:02:57 +02:00
|
|
|
{
|
2016-10-13 01:48:45 +02:00
|
|
|
if (!(key = mpd_parse_kv (data->vector[i], &value)))
|
2016-10-13 01:41:28 +02:00
|
|
|
continue;
|
|
|
|
if (!strcasecmp_ascii (key, "playlistlength")
|
|
|
|
&& xstrtoul (&n, value, 10))
|
2017-01-29 15:26:55 +01:00
|
|
|
item_list_resize (&g.playlist, n);
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_set (map, key, xstrdup (value));
|
2016-10-12 13:11:17 +02:00
|
|
|
}
|
2016-10-10 16:02:57 +02:00
|
|
|
|
2016-10-13 01:41:28 +02:00
|
|
|
// It's much better to process the playlist from the back
|
2017-01-29 18:18:58 +01:00
|
|
|
struct str_map item;
|
|
|
|
str_map_init (&item);
|
|
|
|
item.key_xfrm = tolower_ascii_strxfrm;
|
2016-10-13 01:41:28 +02:00
|
|
|
for (size_t i = data->len - 1; i-- && data->vector[i]; )
|
2016-10-05 01:49:06 +02:00
|
|
|
{
|
2016-10-13 01:48:45 +02:00
|
|
|
if (!(key = mpd_parse_kv (data->vector[i], &value)))
|
2016-10-10 16:02:57 +02:00
|
|
|
continue;
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_set (&item, key, value);
|
2016-10-10 16:02:57 +02:00
|
|
|
if (!strcasecmp_ascii (key, "file"))
|
|
|
|
{
|
2017-01-29 18:18:58 +01:00
|
|
|
if (xstrtoul_map (&item, "pos", &n))
|
|
|
|
item_list_set (&g.playlist, n, &item);
|
|
|
|
str_map_clear (&item);
|
2016-10-10 16:02:57 +02:00
|
|
|
}
|
2016-10-05 01:49:06 +02:00
|
|
|
}
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_free (&item);
|
2016-10-05 01:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_on_info_response (const struct mpd_response *response,
|
2017-01-23 23:32:30 +01:00
|
|
|
const struct strv *data, void *user_data)
|
2016-10-05 01:49:06 +02:00
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
// TODO: preset an error player state?
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_clear (&g.playback_info);
|
2016-10-13 01:41:28 +02:00
|
|
|
if (!response->success)
|
2017-06-04 23:58:04 +02:00
|
|
|
print_error ("%s: %s",
|
|
|
|
"MPD status retrieval failed", response->message_text);
|
2016-10-13 01:41:28 +02:00
|
|
|
else if (!data->len)
|
|
|
|
print_debug ("empty MPD status response");
|
|
|
|
else
|
|
|
|
mpd_process_info (data);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-05 01:49:06 +02:00
|
|
|
mpd_update_playback_state ();
|
2016-10-10 16:02:57 +02:00
|
|
|
current_tab_update ();
|
2016-10-05 01:49:06 +02:00
|
|
|
info_tab_update ();
|
2016-10-01 23:43:51 +02:00
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-01 23:43:51 +02:00
|
|
|
static void
|
|
|
|
mpd_on_tick (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
2017-01-29 15:26:55 +01:00
|
|
|
int64_t diff_msec = clock_msec (CLOCK_BEST) - g.elapsed_since;
|
2016-10-04 18:33:14 +02:00
|
|
|
int elapsed_sec = diff_msec / 1000;
|
|
|
|
int elapsed_msec = diff_msec % 1000;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
g.song_elapsed += elapsed_sec;
|
|
|
|
g.elapsed_since += elapsed_sec * 1000;
|
|
|
|
poller_timer_set (&g.elapsed_event, 1000 - elapsed_msec);
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_request_info (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-13 01:41:28 +02:00
|
|
|
mpd_client_list_ok_begin (c);
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_client_send_command (c, "status", NULL);
|
2017-01-29 15:26:55 +01:00
|
|
|
char *last_version = xstrdup_printf ("%" PRIu32, g.playlist_version);
|
2016-10-10 16:02:57 +02:00
|
|
|
mpd_client_send_command (c, "plchanges", last_version, NULL);
|
|
|
|
free (last_version);
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_client_list_end (c);
|
|
|
|
mpd_client_add_task (c, mpd_on_info_response, NULL);
|
|
|
|
mpd_client_idle (c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_on_events (unsigned subsystems, void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-12 15:08:39 +02:00
|
|
|
if (subsystems & MPD_SUBSYSTEM_DATABASE)
|
|
|
|
library_tab_reload (NULL);
|
|
|
|
|
2017-06-04 23:08:36 +02:00
|
|
|
if (subsystems & (MPD_SUBSYSTEM_PLAYER | MPD_SUBSYSTEM_OPTIONS
|
2017-06-05 00:18:53 +02:00
|
|
|
| MPD_SUBSYSTEM_PLAYLIST | MPD_SUBSYSTEM_MIXER | MPD_SUBSYSTEM_UPDATE))
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_request_info ();
|
|
|
|
else
|
|
|
|
mpd_client_idle (c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_queue_reconnect (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_set (&g.connect_event, 5 * 1000);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_on_password_response (const struct mpd_response *response,
|
2017-01-23 23:32:30 +01:00
|
|
|
const struct strv *data, void *user_data)
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
|
|
|
(void) data;
|
|
|
|
(void) user_data;
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
if (response->success)
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_request_info ();
|
2016-10-12 15:08:39 +02:00
|
|
|
library_tab_reload (NULL);
|
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
print_error ("%s: %s",
|
2017-06-04 23:58:04 +02:00
|
|
|
"MPD authentication failed", response->message_text);
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_client_send_command (c, "close", NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_on_connected (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
const char *password =
|
2017-01-29 15:26:55 +01:00
|
|
|
get_config_string (g.config.root, "settings.password");
|
2016-09-28 22:44:59 +02:00
|
|
|
if (password)
|
|
|
|
{
|
|
|
|
mpd_client_send_command (c, "password", password, NULL);
|
|
|
|
mpd_client_add_task (c, mpd_on_password_response, NULL);
|
|
|
|
}
|
|
|
|
else
|
2016-10-12 15:08:39 +02:00
|
|
|
{
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_request_info ();
|
2016-10-12 15:08:39 +02:00
|
|
|
library_tab_reload (NULL);
|
|
|
|
}
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpd_on_failure (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
// This is also triggered both by a failed connect and a clean disconnect
|
2017-01-29 14:56:37 +01:00
|
|
|
print_debug ("connection to MPD failed");
|
2016-09-28 22:44:59 +02:00
|
|
|
mpd_queue_reconnect ();
|
2017-01-28 01:04:34 +01:00
|
|
|
|
2017-01-29 18:18:58 +01:00
|
|
|
str_map_clear (&g.playback_info);
|
2017-06-05 18:50:50 +02:00
|
|
|
item_list_resize (&g.playlist, 0);
|
2017-01-29 14:56:37 +01:00
|
|
|
|
2017-06-05 18:50:50 +02:00
|
|
|
mpd_update_playback_state ();
|
2017-01-29 14:56:37 +01:00
|
|
|
current_tab_update ();
|
|
|
|
info_tab_update ();
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static void
|
|
|
|
mpd_on_io_hook (void *user_data, bool outgoing, const char *line)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
if (outgoing)
|
2017-06-05 20:09:11 +02:00
|
|
|
debug_tab_push (xstrdup_printf ("<< %s", line), APP_ATTR (OUTGOING));
|
2016-10-05 01:12:31 +02:00
|
|
|
else
|
2017-06-05 20:09:11 +02:00
|
|
|
debug_tab_push (xstrdup_printf (">> %s", line), APP_ATTR (INCOMING));
|
2016-10-05 01:12:31 +02:00
|
|
|
}
|
2016-10-03 19:43:51 +02:00
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
|
|
|
app_on_reconnect (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
struct mpd_client *c = &g.client;
|
2016-09-28 22:44:59 +02:00
|
|
|
c->on_failure = mpd_on_failure;
|
|
|
|
c->on_connected = mpd_on_connected;
|
|
|
|
c->on_event = mpd_on_events;
|
2016-10-04 06:47:30 +02:00
|
|
|
|
|
|
|
if (g_debug_mode)
|
|
|
|
c->on_io_hook = mpd_on_io_hook;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
// We accept hostname/IPv4/IPv6 in pseudo-URL format, as well as sockets
|
2017-01-29 15:26:55 +01:00
|
|
|
char *address = xstrdup (get_config_string (g.config.root,
|
2016-09-28 22:44:59 +02:00
|
|
|
"settings.address")), *p = address, *host = address, *port = "6600";
|
|
|
|
|
|
|
|
// Unwrap IPv6 addresses in format_host_port_pair() format
|
|
|
|
char *right_bracket = strchr (p, ']');
|
|
|
|
if (p[0] == '[' && right_bracket)
|
|
|
|
{
|
|
|
|
*right_bracket = '\0';
|
|
|
|
host = p + 1;
|
|
|
|
p = right_bracket + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *colon = strchr (p, ':');
|
|
|
|
if (colon)
|
|
|
|
{
|
|
|
|
*colon = '\0';
|
|
|
|
port = colon + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct error *e = NULL;
|
|
|
|
if (!mpd_client_connect (c, host, port, &e))
|
|
|
|
{
|
|
|
|
print_error ("%s: %s", "cannot connect to MPD", e->message);
|
|
|
|
error_free (e);
|
|
|
|
mpd_queue_reconnect ();
|
|
|
|
}
|
|
|
|
free (address);
|
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
// --- Signals -----------------------------------------------------------------
|
2016-10-02 06:34:00 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
|
2016-10-03 19:43:51 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
/// Program termination has been requested by a signal
|
|
|
|
static volatile sig_atomic_t g_termination_requested;
|
|
|
|
/// The window has changed in size
|
|
|
|
static volatile sig_atomic_t g_winch_received;
|
2016-10-02 06:34:00 +02:00
|
|
|
|
|
|
|
static void
|
2016-10-05 01:12:31 +02:00
|
|
|
signals_postpone_handling (char id)
|
2016-10-02 06:34:00 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
int original_errno = errno;
|
|
|
|
if (write (g_signal_pipe[1], &id, 1) == -1)
|
|
|
|
soft_assert (errno == EAGAIN);
|
|
|
|
errno = original_errno;
|
2016-10-03 19:43:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-05 01:12:31 +02:00
|
|
|
signals_superhandler (int signum)
|
2016-10-03 19:43:51 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
switch (signum)
|
2016-10-03 19:43:51 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
case SIGWINCH:
|
|
|
|
g_winch_received = true;
|
|
|
|
signals_postpone_handling ('w');
|
|
|
|
break;
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
g_termination_requested = true;
|
|
|
|
signals_postpone_handling ('t');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hard_assert (!"unhandled signal");
|
2016-10-03 19:43:51 +02:00
|
|
|
}
|
2016-10-02 06:34:00 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
static void
|
|
|
|
signals_setup_handlers (void)
|
2016-10-02 06:34:00 +02:00
|
|
|
{
|
2016-10-05 01:12:31 +02:00
|
|
|
if (pipe (g_signal_pipe) == -1)
|
|
|
|
exit_fatal ("%s: %s", "pipe", strerror (errno));
|
2016-10-02 06:34:00 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
set_cloexec (g_signal_pipe[0]);
|
|
|
|
set_cloexec (g_signal_pipe[1]);
|
2016-10-02 06:34:00 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
// So that the pipe cannot overflow; it would make write() block within
|
|
|
|
// the signal handler, which is something we really don't want to happen.
|
|
|
|
// The same holds true for read().
|
|
|
|
set_blocking (g_signal_pipe[0], false);
|
|
|
|
set_blocking (g_signal_pipe[1], false);
|
2016-10-03 19:43:51 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
signal (SIGPIPE, SIG_IGN);
|
2016-10-03 19:43:51 +02:00
|
|
|
|
2016-10-05 01:12:31 +02:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = signals_superhandler;
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
|
|
|
|
if (sigaction (SIGWINCH, &sa, NULL) == -1
|
|
|
|
|| sigaction (SIGINT, &sa, NULL) == -1
|
|
|
|
|| sigaction (SIGTERM, &sa, NULL) == -1)
|
|
|
|
exit_fatal ("sigaction: %s", strerror (errno));
|
2016-10-03 19:43:51 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
// --- Initialisation, event handling ------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_on_tty_readable (const struct pollfd *fd, void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
|
|
|
|
print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_reset (&g.tk_timer);
|
|
|
|
termo_advisereadable (g.tk);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
termo_key_t event;
|
2016-10-13 14:03:17 +02:00
|
|
|
int64_t event_ts = clock_msec (CLOCK_BEST);
|
2016-09-28 22:44:59 +02:00
|
|
|
termo_result_t res;
|
2017-01-29 15:26:55 +01:00
|
|
|
while ((res = termo_getkey (g.tk, &event)) == TERMO_RES_KEY)
|
2016-10-13 14:03:17 +02:00
|
|
|
{
|
|
|
|
// Simple double click detection via release--press delay, only a bit
|
|
|
|
// complicated by the fact that we don't know what's being released
|
|
|
|
static termo_key_t last_event;
|
|
|
|
static int64_t last_event_ts;
|
|
|
|
static int last_button;
|
|
|
|
|
|
|
|
int y, x, button, y_last, x_last;
|
|
|
|
termo_mouse_event_t type, type_last;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (termo_interpret_mouse (g.tk, &event, &type, &button, &y, &x))
|
2016-10-13 14:03:17 +02:00
|
|
|
{
|
|
|
|
bool double_click = termo_interpret_mouse
|
2017-01-29 15:26:55 +01:00
|
|
|
(g.tk, &last_event, &type_last, NULL, &y_last, &x_last)
|
2016-10-13 14:03:17 +02:00
|
|
|
&& event_ts - last_event_ts < 500
|
|
|
|
&& type_last == TERMO_MOUSE_RELEASE && type == TERMO_MOUSE_PRESS
|
|
|
|
&& y_last == y && x_last == x && last_button == button;
|
|
|
|
if (!app_process_mouse (type, y, x, button, double_click))
|
|
|
|
beep ();
|
2016-11-20 23:15:28 +01:00
|
|
|
|
|
|
|
// Prevent interpreting triple clicks as two double clicks
|
|
|
|
if (double_click)
|
|
|
|
last_button = 0;
|
|
|
|
else if (type == TERMO_MOUSE_PRESS)
|
2016-10-13 14:03:17 +02:00
|
|
|
last_button = button;
|
|
|
|
}
|
|
|
|
else if (!app_process_termo_event (&event))
|
2016-10-07 00:36:43 +02:00
|
|
|
beep ();
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2016-10-13 14:03:17 +02:00
|
|
|
last_event = event;
|
|
|
|
last_event_ts = event_ts;
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
if (res == TERMO_RES_AGAIN)
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_set (&g.tk_timer, termo_get_waittime (g.tk));
|
2016-09-28 22:44:59 +02:00
|
|
|
else if (res == TERMO_RES_ERROR || res == TERMO_RES_EOF)
|
|
|
|
app_quit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_on_key_timer (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
termo_key_t event;
|
2017-01-29 15:26:55 +01:00
|
|
|
if (termo_getkey_force (g.tk, &event) == TERMO_RES_KEY)
|
2016-09-28 22:44:59 +02:00
|
|
|
if (!app_process_termo_event (&event))
|
|
|
|
app_quit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_on_signal_pipe_readable (const struct pollfd *fd, void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
char id = 0;
|
|
|
|
(void) read (fd->fd, &id, 1);
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g_termination_requested && !g.quitting)
|
2016-09-28 22:44:59 +02:00
|
|
|
app_quit ();
|
|
|
|
|
|
|
|
if (g_winch_received)
|
|
|
|
{
|
2017-06-03 23:03:38 +02:00
|
|
|
g_winch_received = false;
|
2016-09-28 22:44:59 +02:00
|
|
|
update_curses_terminal_size ();
|
2016-10-04 23:39:32 +02:00
|
|
|
app_invalidate ();
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
static void
|
|
|
|
app_on_message_timer (void *user_data)
|
|
|
|
{
|
|
|
|
(void) user_data;
|
|
|
|
|
|
|
|
free (g.message);
|
|
|
|
g.message = NULL;
|
|
|
|
app_invalidate ();
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:44:59 +02:00
|
|
|
static void
|
|
|
|
app_log_handler (void *user_data, const char *quote, const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
// We certainly don't want to end up in a possibly infinite recursion
|
|
|
|
static bool in_processing;
|
|
|
|
if (in_processing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
in_processing = true;
|
|
|
|
|
|
|
|
struct str message;
|
|
|
|
str_init (&message);
|
|
|
|
str_append (&message, quote);
|
|
|
|
str_append_vprintf (&message, fmt, ap);
|
|
|
|
|
|
|
|
// If the standard error output isn't redirected, try our best at showing
|
2017-06-04 23:58:04 +02:00
|
|
|
// the message to the user
|
2016-10-02 06:34:00 +02:00
|
|
|
if (!isatty (STDERR_FILENO))
|
|
|
|
fprintf (stderr, "%s\n", message.str);
|
|
|
|
else if (g_debug_tab.active)
|
2017-06-05 20:09:11 +02:00
|
|
|
debug_tab_push (str_steal (&message),
|
2017-01-29 15:26:55 +01:00
|
|
|
user_data == NULL ? 0 : g.attrs[(intptr_t) user_data].attrs);
|
2016-10-02 06:34:00 +02:00
|
|
|
else
|
2016-09-28 22:44:59 +02:00
|
|
|
{
|
2017-06-04 23:58:04 +02:00
|
|
|
free (g.message);
|
|
|
|
g.message = xstrdup (message.str);
|
|
|
|
app_invalidate ();
|
|
|
|
poller_timer_set (&g.message_timer, 5000);
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
str_free (&message);
|
|
|
|
|
|
|
|
in_processing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_init_poller_events (void)
|
|
|
|
{
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_fd_init (&g.signal_event, &g.poller, g_signal_pipe[0]);
|
|
|
|
g.signal_event.dispatcher = app_on_signal_pipe_readable;
|
|
|
|
poller_fd_set (&g.signal_event, POLLIN);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_fd_init (&g.tty_event, &g.poller, STDIN_FILENO);
|
|
|
|
g.tty_event.dispatcher = app_on_tty_readable;
|
|
|
|
poller_fd_set (&g.tty_event, POLLIN);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-06-04 23:58:04 +02:00
|
|
|
poller_timer_init (&g.message_timer, &g.poller);
|
|
|
|
g.message_timer.dispatcher = app_on_message_timer;
|
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_init (&g.tk_timer, &g.poller);
|
|
|
|
g.tk_timer.dispatcher = app_on_key_timer;
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_init (&g.connect_event, &g.poller);
|
|
|
|
g.connect_event.dispatcher = app_on_reconnect;
|
|
|
|
poller_timer_set (&g.connect_event, 0);
|
2016-10-01 23:43:51 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_timer_init (&g.elapsed_event, &g.poller);
|
|
|
|
g.elapsed_event.dispatcher = mpd_on_tick;
|
2016-10-04 23:39:32 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
poller_idle_init (&g.refresh_event, &g.poller);
|
|
|
|
g.refresh_event.dispatcher = app_on_refresh;
|
2016-09-28 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
static const struct opt opts[] =
|
|
|
|
{
|
|
|
|
{ 'd', "debug", NULL, 0, "run in debug mode" },
|
|
|
|
{ 'h', "help", NULL, 0, "display this help and exit" },
|
|
|
|
{ 'V', "version", NULL, 0, "output version information and exit" },
|
|
|
|
{ 0, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct opt_handler oh;
|
|
|
|
opt_handler_init (&oh, argc, argv, opts, NULL, "MPD client.");
|
|
|
|
|
|
|
|
int c;
|
|
|
|
while ((c = opt_handler_get (&oh)) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
g_debug_mode = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
opt_handler_usage (&oh, stdout);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'V':
|
|
|
|
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
default:
|
|
|
|
print_error ("wrong options");
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
{
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
opt_handler_free (&oh);
|
|
|
|
|
|
|
|
// We only need to convert to and from the terminal encoding
|
|
|
|
if (!setlocale (LC_CTYPE, ""))
|
|
|
|
print_warning ("failed to set the locale");
|
|
|
|
|
|
|
|
app_init_context ();
|
|
|
|
app_load_configuration ();
|
|
|
|
app_init_terminal ();
|
2016-10-10 11:13:52 +02:00
|
|
|
signals_setup_handlers ();
|
|
|
|
app_init_poller_events ();
|
2017-01-24 16:23:20 +01:00
|
|
|
app_init_bindings ();
|
2016-10-02 06:34:00 +02:00
|
|
|
|
|
|
|
if (g_debug_mode)
|
2016-10-10 11:13:52 +02:00
|
|
|
app_prepend_tab (debug_tab_init ());
|
2016-10-02 06:34:00 +02:00
|
|
|
|
2016-10-10 11:13:52 +02:00
|
|
|
// Redirect all messages from liberty to a special tab so they're not lost
|
2016-09-28 22:44:59 +02:00
|
|
|
g_log_message_real = app_log_handler;
|
|
|
|
|
2016-10-10 11:13:52 +02:00
|
|
|
app_prepend_tab (info_tab_init ());
|
2017-01-29 15:26:55 +01:00
|
|
|
if (g.streams.len)
|
2016-10-10 11:13:52 +02:00
|
|
|
app_prepend_tab (streams_tab_init ());
|
2016-10-12 15:08:39 +02:00
|
|
|
app_prepend_tab (library_tab_init ());
|
2016-10-10 16:02:57 +02:00
|
|
|
app_prepend_tab (current_tab_init ());
|
2017-01-29 15:26:55 +01:00
|
|
|
app_switch_tab ((g.help_tab = help_tab_init ()));
|
2016-09-28 22:44:59 +02:00
|
|
|
|
2017-01-29 15:26:55 +01:00
|
|
|
g.polling = true;
|
|
|
|
while (g.polling)
|
|
|
|
poller_run (&g.poller);
|
2016-09-28 22:44:59 +02:00
|
|
|
|
|
|
|
endwin ();
|
|
|
|
g_log_message_real = log_message_stdio;
|
|
|
|
app_free_context ();
|
|
|
|
return 0;
|
|
|
|
}
|