mpd_client.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * MPD connection on the GLib main loop. The connection sits in "idle" so
  3. * changes arrive as events instead of being polled; anything else first leaves
  4. * idle, does its work and goes back.
  5. */
  6. #include <glib-unix.h>
  7. #include <mpd/client.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "app.h"
  11. #define CONNECT_TIMEOUT_MS 2000
  12. #define RECONNECT_S 3
  13. struct mpdc {
  14. char *host;
  15. unsigned int port;
  16. struct radio_state *state;
  17. mpdc_changed_fn changed;
  18. mpdc_log_fn log;
  19. void *user;
  20. struct mpd_connection *conn;
  21. guint watch_id;
  22. guint retry_id;
  23. bool was_connected;
  24. };
  25. static gboolean on_readable(gint fd, GIOCondition cond, gpointer data);
  26. static gboolean try_connect(gpointer data);
  27. static void note(struct mpdc *m, const char *text)
  28. {
  29. m->log(m->user, text);
  30. }
  31. static void drop_connection(struct mpdc *m, const char *why)
  32. {
  33. char buf[200];
  34. if (m->watch_id) {
  35. g_source_remove(m->watch_id);
  36. m->watch_id = 0;
  37. }
  38. if (m->conn) {
  39. mpd_connection_free(m->conn);
  40. m->conn = NULL;
  41. }
  42. m->state->connected = false;
  43. if (m->was_connected && why) {
  44. snprintf(buf, sizeof(buf), "MPD: %s", why);
  45. note(m, buf);
  46. }
  47. m->was_connected = false;
  48. if (!m->retry_id)
  49. m->retry_id = g_timeout_add_seconds(RECONNECT_S, try_connect, m);
  50. m->changed(m->user);
  51. }
  52. /*
  53. * Returns false if the connection was lost. A command MPD merely refused,
  54. * such as changing the volume with no mixer, does not end the session.
  55. */
  56. static bool check_connection(struct mpdc *m)
  57. {
  58. char buf[256];
  59. switch (mpd_connection_get_error(m->conn)) {
  60. case MPD_ERROR_SUCCESS:
  61. return true;
  62. case MPD_ERROR_SERVER:
  63. snprintf(buf, sizeof(buf), "MPD: %s",
  64. mpd_connection_get_error_message(m->conn));
  65. note(m, buf);
  66. if (mpd_connection_clear_error(m->conn))
  67. return true;
  68. break;
  69. default:
  70. break;
  71. }
  72. g_strlcpy(buf, mpd_connection_get_error_message(m->conn), sizeof(buf));
  73. drop_connection(m, buf);
  74. return false;
  75. }
  76. static void format_title(char *out, size_t size, const struct mpd_song *song)
  77. {
  78. const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
  79. const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
  80. const char *name = mpd_song_get_tag(song, MPD_TAG_NAME, 0);
  81. const char *uri = mpd_song_get_uri(song);
  82. const char *slash;
  83. if (artist && title) {
  84. snprintf(out, size, "%s - %s", artist, title);
  85. } else if (title && name) { /* a stream announcing what it plays */
  86. snprintf(out, size, "%s: %s", name, title);
  87. } else if (title || name) {
  88. g_strlcpy(out, title ? title : name, size);
  89. } else {
  90. slash = strrchr(uri, '/');
  91. g_strlcpy(out, (slash && slash[1] && !strstr(uri, "://")) ? slash + 1 : uri, size);
  92. }
  93. }
  94. static void refresh(struct mpdc *m)
  95. {
  96. struct radio_state *s = m->state;
  97. struct mpd_status *status;
  98. struct mpd_song *song;
  99. status = mpd_run_status(m->conn);
  100. if (!status) {
  101. check_connection(m);
  102. return;
  103. }
  104. switch (mpd_status_get_state(status)) {
  105. case MPD_STATE_PLAY: s->state = PLAY_PLAYING; break;
  106. case MPD_STATE_PAUSE: s->state = PLAY_PAUSED; break;
  107. case MPD_STATE_STOP: s->state = PLAY_STOPPED; break;
  108. default: s->state = PLAY_UNKNOWN; break;
  109. }
  110. s->song_pos = mpd_status_get_song_pos(status);
  111. s->queue_len = mpd_status_get_queue_length(status);
  112. s->elapsed_ms = mpd_status_get_elapsed_ms(status);
  113. s->elapsed_stamp_us = g_get_monotonic_time();
  114. s->total_ms = mpd_status_get_total_time(status) * 1000;
  115. s->volume = mpd_status_get_volume(status);
  116. s->repeat = mpd_status_get_repeat(status);
  117. s->random = mpd_status_get_random(status);
  118. mpd_status_free(status);
  119. s->title[0] = '\0';
  120. /* as the firmware: a stopped player shows no title */
  121. if (s->state != PLAY_STOPPED) {
  122. song = mpd_run_current_song(m->conn);
  123. if (song) {
  124. format_title(s->title, sizeof(s->title), song);
  125. mpd_song_free(song);
  126. } else if (!check_connection(m)) {
  127. return;
  128. }
  129. }
  130. m->changed(m->user);
  131. }
  132. static void enter_idle(struct mpdc *m)
  133. {
  134. if (!m->conn)
  135. return;
  136. if (!mpd_send_idle_mask(m->conn, MPD_IDLE_QUEUE | MPD_IDLE_PLAYER |
  137. MPD_IDLE_MIXER | MPD_IDLE_OPTIONS))
  138. check_connection(m);
  139. }
  140. static gboolean on_readable(gint fd, GIOCondition cond, gpointer data)
  141. {
  142. struct mpdc *m = data;
  143. (void)fd;
  144. if (cond & (G_IO_HUP | G_IO_ERR)) {
  145. m->watch_id = 0;
  146. drop_connection(m, "connection closed");
  147. return G_SOURCE_REMOVE;
  148. }
  149. mpd_recv_idle(m->conn, false);
  150. if (!check_connection(m))
  151. return G_SOURCE_REMOVE; /* drop_connection removed this source */
  152. refresh(m);
  153. if (m->conn)
  154. enter_idle(m);
  155. return m->conn ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE;
  156. }
  157. static gboolean try_connect(gpointer data)
  158. {
  159. struct mpdc *m = data;
  160. struct mpd_connection *c;
  161. char buf[200];
  162. m->retry_id = 0;
  163. c = mpd_connection_new(m->host, m->port, CONNECT_TIMEOUT_MS);
  164. if (!c || mpd_connection_get_error(c) != MPD_ERROR_SUCCESS) {
  165. if (c)
  166. mpd_connection_free(c);
  167. m->retry_id = g_timeout_add_seconds(RECONNECT_S, try_connect, m);
  168. return G_SOURCE_REMOVE;
  169. }
  170. m->conn = c;
  171. m->state->connected = true;
  172. m->was_connected = true;
  173. snprintf(buf, sizeof(buf), "Połączono z MPD: %s", m->host);
  174. note(m, buf);
  175. refresh(m);
  176. if (m->conn) {
  177. m->watch_id = g_unix_fd_add(mpd_connection_get_fd(m->conn),
  178. G_IO_IN | G_IO_HUP | G_IO_ERR,
  179. on_readable, m);
  180. enter_idle(m);
  181. }
  182. return G_SOURCE_REMOVE;
  183. }
  184. void mpdc_command(struct mpdc *m, enum mpdc_command cmd)
  185. {
  186. bool ok = true;
  187. if (!m->conn) {
  188. note(m, "Brak połączenia z MPD");
  189. return;
  190. }
  191. /* leave idle; any event it reports is covered by the refresh below */
  192. mpd_run_noidle(m->conn);
  193. if (!check_connection(m))
  194. return;
  195. switch (cmd) {
  196. case MPDC_PLAY: ok = mpd_run_play(m->conn); break;
  197. case MPDC_TOGGLE: ok = mpd_run_toggle_pause(m->conn); break;
  198. case MPDC_STOP: ok = mpd_run_stop(m->conn); break;
  199. case MPDC_NEXT: ok = mpd_run_next(m->conn); break;
  200. case MPDC_PREV: ok = mpd_run_previous(m->conn); break;
  201. case MPDC_VOLUME_UP: ok = mpd_run_change_volume(m->conn, 1); break;
  202. case MPDC_VOLUME_DOWN: ok = mpd_run_change_volume(m->conn, -1); break;
  203. case MPDC_REFRESH: break;
  204. }
  205. if (!ok && !check_connection(m))
  206. return;
  207. refresh(m);
  208. if (m->conn)
  209. enter_idle(m);
  210. }
  211. struct mpdc *mpdc_new(const char *host, unsigned int port, struct radio_state *state,
  212. mpdc_changed_fn changed, mpdc_log_fn log, void *user)
  213. {
  214. struct mpdc *m = g_new0(struct mpdc, 1);
  215. m->host = g_strdup(host);
  216. m->port = port;
  217. m->state = state;
  218. m->changed = changed;
  219. m->log = log;
  220. m->user = user;
  221. /* not directly: the owner must finish construction first */
  222. m->retry_id = g_idle_add(try_connect, m);
  223. return m;
  224. }
  225. void mpdc_free(struct mpdc *m)
  226. {
  227. if (m->watch_id)
  228. g_source_remove(m->watch_id);
  229. if (m->retry_id)
  230. g_source_remove(m->retry_id);
  231. if (m->conn)
  232. mpd_connection_free(m->conn);
  233. g_free(m->host);
  234. g_free(m);
  235. }