| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /*
- * MPD connection on the GLib main loop. The connection sits in "idle" so
- * changes arrive as events instead of being polled; anything else first leaves
- * idle, does its work and goes back.
- */
- #include <glib-unix.h>
- #include <mpd/client.h>
- #include <stdio.h>
- #include <string.h>
- #include "app.h"
- #define CONNECT_TIMEOUT_MS 2000
- #define RECONNECT_S 3
- struct mpdc {
- char *host;
- unsigned int port;
- struct radio_state *state;
- mpdc_changed_fn changed;
- mpdc_log_fn log;
- void *user;
- struct mpd_connection *conn;
- guint watch_id;
- guint retry_id;
- bool was_connected;
- };
- static gboolean on_readable(gint fd, GIOCondition cond, gpointer data);
- static gboolean try_connect(gpointer data);
- static void note(struct mpdc *m, const char *text)
- {
- m->log(m->user, text);
- }
- static void drop_connection(struct mpdc *m, const char *why)
- {
- char buf[200];
- if (m->watch_id) {
- g_source_remove(m->watch_id);
- m->watch_id = 0;
- }
- if (m->conn) {
- mpd_connection_free(m->conn);
- m->conn = NULL;
- }
- m->state->connected = false;
- if (m->was_connected && why) {
- snprintf(buf, sizeof(buf), "MPD: %s", why);
- note(m, buf);
- }
- m->was_connected = false;
- if (!m->retry_id)
- m->retry_id = g_timeout_add_seconds(RECONNECT_S, try_connect, m);
- m->changed(m->user);
- }
- /*
- * Returns false if the connection was lost. A command MPD merely refused,
- * such as changing the volume with no mixer, does not end the session.
- */
- static bool check_connection(struct mpdc *m)
- {
- char buf[256];
- switch (mpd_connection_get_error(m->conn)) {
- case MPD_ERROR_SUCCESS:
- return true;
- case MPD_ERROR_SERVER:
- snprintf(buf, sizeof(buf), "MPD: %s",
- mpd_connection_get_error_message(m->conn));
- note(m, buf);
- if (mpd_connection_clear_error(m->conn))
- return true;
- break;
- default:
- break;
- }
- g_strlcpy(buf, mpd_connection_get_error_message(m->conn), sizeof(buf));
- drop_connection(m, buf);
- return false;
- }
- static void format_title(char *out, size_t size, const struct mpd_song *song)
- {
- const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
- const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
- const char *name = mpd_song_get_tag(song, MPD_TAG_NAME, 0);
- const char *uri = mpd_song_get_uri(song);
- const char *slash;
- if (artist && title) {
- snprintf(out, size, "%s - %s", artist, title);
- } else if (title && name) { /* a stream announcing what it plays */
- snprintf(out, size, "%s: %s", name, title);
- } else if (title || name) {
- g_strlcpy(out, title ? title : name, size);
- } else {
- slash = strrchr(uri, '/');
- g_strlcpy(out, (slash && slash[1] && !strstr(uri, "://")) ? slash + 1 : uri, size);
- }
- }
- static void refresh(struct mpdc *m)
- {
- struct radio_state *s = m->state;
- struct mpd_status *status;
- struct mpd_song *song;
- status = mpd_run_status(m->conn);
- if (!status) {
- check_connection(m);
- return;
- }
- switch (mpd_status_get_state(status)) {
- case MPD_STATE_PLAY: s->state = PLAY_PLAYING; break;
- case MPD_STATE_PAUSE: s->state = PLAY_PAUSED; break;
- case MPD_STATE_STOP: s->state = PLAY_STOPPED; break;
- default: s->state = PLAY_UNKNOWN; break;
- }
- s->song_pos = mpd_status_get_song_pos(status);
- s->queue_len = mpd_status_get_queue_length(status);
- s->elapsed_ms = mpd_status_get_elapsed_ms(status);
- s->elapsed_stamp_us = g_get_monotonic_time();
- s->total_ms = mpd_status_get_total_time(status) * 1000;
- s->volume = mpd_status_get_volume(status);
- s->repeat = mpd_status_get_repeat(status);
- s->random = mpd_status_get_random(status);
- mpd_status_free(status);
- s->title[0] = '\0';
- /* as the firmware: a stopped player shows no title */
- if (s->state != PLAY_STOPPED) {
- song = mpd_run_current_song(m->conn);
- if (song) {
- format_title(s->title, sizeof(s->title), song);
- mpd_song_free(song);
- } else if (!check_connection(m)) {
- return;
- }
- }
- m->changed(m->user);
- }
- static void enter_idle(struct mpdc *m)
- {
- if (!m->conn)
- return;
- if (!mpd_send_idle_mask(m->conn, MPD_IDLE_QUEUE | MPD_IDLE_PLAYER |
- MPD_IDLE_MIXER | MPD_IDLE_OPTIONS))
- check_connection(m);
- }
- static gboolean on_readable(gint fd, GIOCondition cond, gpointer data)
- {
- struct mpdc *m = data;
- (void)fd;
- if (cond & (G_IO_HUP | G_IO_ERR)) {
- m->watch_id = 0;
- drop_connection(m, "connection closed");
- return G_SOURCE_REMOVE;
- }
- mpd_recv_idle(m->conn, false);
- if (!check_connection(m))
- return G_SOURCE_REMOVE; /* drop_connection removed this source */
- refresh(m);
- if (m->conn)
- enter_idle(m);
- return m->conn ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE;
- }
- static gboolean try_connect(gpointer data)
- {
- struct mpdc *m = data;
- struct mpd_connection *c;
- char buf[200];
- m->retry_id = 0;
- c = mpd_connection_new(m->host, m->port, CONNECT_TIMEOUT_MS);
- if (!c || mpd_connection_get_error(c) != MPD_ERROR_SUCCESS) {
- if (c)
- mpd_connection_free(c);
- m->retry_id = g_timeout_add_seconds(RECONNECT_S, try_connect, m);
- return G_SOURCE_REMOVE;
- }
- m->conn = c;
- m->state->connected = true;
- m->was_connected = true;
- snprintf(buf, sizeof(buf), "Połączono z MPD: %s", m->host);
- note(m, buf);
- refresh(m);
- if (m->conn) {
- m->watch_id = g_unix_fd_add(mpd_connection_get_fd(m->conn),
- G_IO_IN | G_IO_HUP | G_IO_ERR,
- on_readable, m);
- enter_idle(m);
- }
- return G_SOURCE_REMOVE;
- }
- void mpdc_command(struct mpdc *m, enum mpdc_command cmd)
- {
- bool ok = true;
- if (!m->conn) {
- note(m, "Brak połączenia z MPD");
- return;
- }
- /* leave idle; any event it reports is covered by the refresh below */
- mpd_run_noidle(m->conn);
- if (!check_connection(m))
- return;
- switch (cmd) {
- case MPDC_PLAY: ok = mpd_run_play(m->conn); break;
- case MPDC_TOGGLE: ok = mpd_run_toggle_pause(m->conn); break;
- case MPDC_STOP: ok = mpd_run_stop(m->conn); break;
- case MPDC_NEXT: ok = mpd_run_next(m->conn); break;
- case MPDC_PREV: ok = mpd_run_previous(m->conn); break;
- case MPDC_VOLUME_UP: ok = mpd_run_change_volume(m->conn, 1); break;
- case MPDC_VOLUME_DOWN: ok = mpd_run_change_volume(m->conn, -1); break;
- case MPDC_REFRESH: break;
- }
- if (!ok && !check_connection(m))
- return;
- refresh(m);
- if (m->conn)
- enter_idle(m);
- }
- struct mpdc *mpdc_new(const char *host, unsigned int port, struct radio_state *state,
- mpdc_changed_fn changed, mpdc_log_fn log, void *user)
- {
- struct mpdc *m = g_new0(struct mpdc, 1);
- m->host = g_strdup(host);
- m->port = port;
- m->state = state;
- m->changed = changed;
- m->log = log;
- m->user = user;
- /* not directly: the owner must finish construction first */
- m->retry_id = g_idle_add(try_connect, m);
- return m;
- }
- void mpdc_free(struct mpdc *m)
- {
- if (m->watch_id)
- g_source_remove(m->watch_id);
- if (m->retry_id)
- g_source_remove(m->retry_id);
- if (m->conn)
- mpd_connection_free(m->conn);
- g_free(m->host);
- g_free(m);
- }
|