1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "mpdclient.h"
21 #include "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "gidle.h"
25 #include "charset.h"
27 #include <mpd/client.h>
29 #include <assert.h>
31 static gboolean
32 mpdclient_enter_idle_callback(gpointer user_data)
33 {
34 struct mpdclient *c = user_data;
35 assert(c->enter_idle_source_id != 0);
36 assert(c->source != NULL);
37 assert(!c->idle);
39 c->enter_idle_source_id = 0;
40 c->idle = mpd_glib_enter(c->source);
41 return false;
42 }
44 static void
45 mpdclient_schedule_enter_idle(struct mpdclient *c)
46 {
47 assert(c != NULL);
48 assert(c->source != NULL);
50 if (c->enter_idle_source_id == 0)
51 /* automatically re-enter MPD "idle" mode */
52 c->enter_idle_source_id =
53 g_idle_add(mpdclient_enter_idle_callback, c);
54 }
56 static void
57 mpdclient_cancel_enter_idle(struct mpdclient *c)
58 {
59 if (c->enter_idle_source_id != 0) {
60 g_source_remove(c->enter_idle_source_id);
61 c->enter_idle_source_id = 0;
62 }
63 }
65 static void
66 mpdclient_invoke_error_callback(enum mpd_error error,
67 const char *message)
68 {
69 char *allocated;
70 if (error == MPD_ERROR_SERVER)
71 /* server errors are UTF-8, the others are locale */
72 message = allocated = utf8_to_locale(message);
73 else
74 allocated = NULL;
76 mpdclient_error_callback(message);
77 g_free(allocated);
78 }
80 static void
81 mpdclient_invoke_error_callback1(struct mpdclient *c)
82 {
83 assert(c != NULL);
84 assert(c->connection != NULL);
86 struct mpd_connection *connection = c->connection;
88 enum mpd_error error = mpd_connection_get_error(connection);
89 assert(error != MPD_ERROR_SUCCESS);
91 mpdclient_invoke_error_callback(error,
92 mpd_connection_get_error_message(connection));
93 }
95 static void
96 mpdclient_gidle_callback(enum mpd_error error,
97 gcc_unused enum mpd_server_error server_error,
98 const char *message, enum mpd_idle events,
99 void *ctx)
100 {
101 struct mpdclient *c = ctx;
103 c->idle = false;
105 assert(mpdclient_is_connected(c));
107 if (error != MPD_ERROR_SUCCESS) {
108 mpdclient_invoke_error_callback(error, message);
109 mpdclient_disconnect(c);
110 mpdclient_lost_callback();
111 return;
112 }
114 c->events |= events;
115 mpdclient_update(c);
117 mpdclient_idle_callback(c->events);
119 c->events = 0;
121 if (c->source != NULL)
122 mpdclient_schedule_enter_idle(c);
123 }
125 /****************************************************************************/
126 /*** mpdclient functions ****************************************************/
127 /****************************************************************************/
129 bool
130 mpdclient_handle_error(struct mpdclient *c)
131 {
132 enum mpd_error error = mpd_connection_get_error(c->connection);
134 assert(error != MPD_ERROR_SUCCESS);
136 if (error == MPD_ERROR_SERVER &&
137 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
138 mpdclient_auth_callback(c))
139 return true;
141 mpdclient_invoke_error_callback(error,
142 mpd_connection_get_error_message(c->connection));
144 if (!mpd_connection_clear_error(c->connection)) {
145 mpdclient_disconnect(c);
146 mpdclient_lost_callback();
147 }
149 return false;
150 }
152 struct mpdclient *
153 mpdclient_new(const gchar *host, unsigned port,
154 unsigned timeout_ms, const gchar *password)
155 {
156 struct mpdclient *c = g_new0(struct mpdclient, 1);
158 c->host = host;
159 c->port = port;
160 c->timeout_ms = timeout_ms;
161 c->password = password;
163 playlist_init(&c->playlist);
164 c->volume = -1;
165 c->events = 0;
166 c->playing = false;
168 return c;
169 }
171 void
172 mpdclient_free(struct mpdclient *c)
173 {
174 mpdclient_disconnect(c);
176 mpdclient_playlist_free(&c->playlist);
178 g_free(c);
179 }
181 static void
182 mpdclient_status_free(struct mpdclient *c)
183 {
184 if (c->status == NULL)
185 return;
187 mpd_status_free(c->status);
188 c->status = NULL;
190 c->volume = -1;
191 c->playing = false;
192 }
194 void
195 mpdclient_disconnect(struct mpdclient *c)
196 {
197 mpdclient_cancel_enter_idle(c);
199 if (c->source != NULL) {
200 mpd_glib_free(c->source);
201 c->source = NULL;
202 c->idle = false;
203 }
205 if (c->connection) {
206 mpd_connection_free(c->connection);
207 ++c->connection_id;
208 }
209 c->connection = NULL;
211 mpdclient_status_free(c);
213 playlist_clear(&c->playlist);
215 if (c->song)
216 c->song = NULL;
218 /* everything has changed after a disconnect */
219 c->events |= MPD_IDLE_ALL;
220 }
222 bool
223 mpdclient_connect(struct mpdclient *c)
224 {
225 /* close any open connection */
226 mpdclient_disconnect(c);
228 /* connect to MPD */
229 c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms);
230 if (c->connection == NULL)
231 g_error("Out of memory");
233 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
234 mpdclient_invoke_error_callback1(c);
235 mpdclient_disconnect(c);
236 mpdclient_failed_callback();
237 return false;
238 }
240 /* send password */
241 if (c->password != NULL &&
242 !mpd_run_password(c->connection, c->password)) {
243 mpdclient_invoke_error_callback1(c);
244 mpdclient_disconnect(c);
245 mpdclient_failed_callback();
246 return false;
247 }
249 c->source = mpd_glib_new(c->connection,
250 mpdclient_gidle_callback, c);
251 mpdclient_schedule_enter_idle(c);
253 ++c->connection_id;
255 mpdclient_connected_callback();
257 return true;
258 }
260 bool
261 mpdclient_update(struct mpdclient *c)
262 {
263 struct mpd_connection *connection = mpdclient_get_connection(c);
265 if (connection == NULL)
266 return false;
268 /* free the old status */
269 mpdclient_status_free(c);
271 /* retrieve new status */
272 c->status = mpd_run_status(connection);
273 if (c->status == NULL)
274 return mpdclient_handle_error(c);
276 c->volume = mpd_status_get_volume(c->status);
277 c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
279 /* check if the playlist needs an update */
280 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
281 bool retval;
283 if (!playlist_is_empty(&c->playlist))
284 retval = mpdclient_playlist_update_changes(c);
285 else
286 retval = mpdclient_playlist_update(c);
287 if (!retval)
288 return false;
289 }
291 /* update the current song */
292 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
293 c->song = playlist_get_song(&c->playlist,
294 mpd_status_get_song_pos(c->status));
295 }
297 return true;
298 }
300 struct mpd_connection *
301 mpdclient_get_connection(struct mpdclient *c)
302 {
303 if (c->source != NULL && c->idle) {
304 c->idle = false;
305 mpd_glib_leave(c->source);
307 mpdclient_schedule_enter_idle(c);
308 }
310 return c->connection;
311 }
313 static struct mpd_status *
314 mpdclient_recv_status(struct mpdclient *c)
315 {
316 assert(c->connection != NULL);
318 struct mpd_status *status = mpd_recv_status(c->connection);
319 if (status == NULL) {
320 mpdclient_handle_error(c);
321 return NULL;
322 }
324 if (c->status != NULL)
325 mpd_status_free(c->status);
326 return c->status = status;
327 }
329 /****************************************************************************/
330 /*** MPD Commands **********************************************************/
331 /****************************************************************************/
333 bool
334 mpdclient_cmd_crop(struct mpdclient *c)
335 {
336 if (!mpdclient_is_playing(c))
337 return false;
339 int length = mpd_status_get_queue_length(c->status);
340 int current = mpd_status_get_song_pos(c->status);
341 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
342 return true;
344 struct mpd_connection *connection = mpdclient_get_connection(c);
345 if (connection == NULL)
346 return false;
348 mpd_command_list_begin(connection, false);
350 if (current < length - 1)
351 mpd_send_delete_range(connection, current + 1, length);
352 if (current > 0)
353 mpd_send_delete_range(connection, 0, current);
355 mpd_command_list_end(connection);
357 return mpdclient_finish_command(c);
358 }
360 bool
361 mpdclient_cmd_clear(struct mpdclient *c)
362 {
363 struct mpd_connection *connection = mpdclient_get_connection(c);
364 if (connection == NULL)
365 return false;
367 /* send "clear" and "status" */
368 if (!mpd_command_list_begin(connection, false) ||
369 !mpd_send_clear(connection) ||
370 !mpd_send_status(connection) ||
371 !mpd_command_list_end(connection))
372 return mpdclient_handle_error(c);
374 /* receive the new status, store it in the mpdclient struct */
376 struct mpd_status *status = mpdclient_recv_status(c);
377 if (status == NULL)
378 return false;
380 if (!mpd_response_finish(connection))
381 return mpdclient_handle_error(c);
383 /* update mpdclient.playlist */
385 if (mpd_status_get_queue_length(status) == 0) {
386 /* after the "clear" command, the queue is really
387 empty - this means we can clear it locally,
388 reducing the UI latency */
389 playlist_clear(&c->playlist);
390 c->playlist.version = mpd_status_get_queue_version(status);
391 c->song = NULL;
392 }
394 c->events |= MPD_IDLE_QUEUE;
395 return true;
396 }
398 bool
399 mpdclient_cmd_volume(struct mpdclient *c, gint value)
400 {
401 struct mpd_connection *connection = mpdclient_get_connection(c);
402 if (connection == NULL)
403 return false;
405 mpd_send_set_volume(connection, value);
406 return mpdclient_finish_command(c);
407 }
409 bool
410 mpdclient_cmd_volume_up(struct mpdclient *c)
411 {
412 struct mpd_connection *connection = mpdclient_get_connection(c);
413 if (connection == NULL)
414 return false;
416 if (c->status == NULL ||
417 mpd_status_get_volume(c->status) == -1)
418 return true;
420 if (c->volume < 0)
421 c->volume = mpd_status_get_volume(c->status);
423 if (c->volume >= 100)
424 return true;
426 return mpdclient_cmd_volume(c, ++c->volume);
427 }
429 bool
430 mpdclient_cmd_volume_down(struct mpdclient *c)
431 {
432 struct mpd_connection *connection = mpdclient_get_connection(c);
433 if (connection == NULL)
434 return false;
436 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
437 return true;
439 if (c->volume < 0)
440 c->volume = mpd_status_get_volume(c->status);
442 if (c->volume <= 0)
443 return true;
445 return mpdclient_cmd_volume(c, --c->volume);
446 }
448 bool
449 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
450 {
451 struct mpd_connection *connection = mpdclient_get_connection(c);
452 if (connection == NULL)
453 return false;
455 return mpd_send_add(connection, path_utf8)?
456 mpdclient_finish_command(c) : false;
457 }
459 bool
460 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
461 {
462 assert(c != NULL);
463 assert(song != NULL);
465 struct mpd_connection *connection = mpdclient_get_connection(c);
466 if (connection == NULL || c->status == NULL)
467 return false;
469 /* send the add command to mpd; at the same time, get the new
470 status (to verify the new playlist id) and the last song
471 (we hope that's the song we just added) */
473 if (!mpd_command_list_begin(connection, true) ||
474 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
475 !mpd_send_status(connection) ||
476 !mpd_send_get_queue_song_pos(connection,
477 playlist_length(&c->playlist)) ||
478 !mpd_command_list_end(connection) ||
479 !mpd_response_next(connection))
480 return mpdclient_handle_error(c);
482 c->events |= MPD_IDLE_QUEUE;
484 struct mpd_status *status = mpdclient_recv_status(c);
485 if (status == NULL)
486 return false;
488 if (!mpd_response_next(connection))
489 return mpdclient_handle_error(c);
491 struct mpd_song *new_song = mpd_recv_song(connection);
492 if (!mpd_response_finish(connection) || new_song == NULL) {
493 if (new_song != NULL)
494 mpd_song_free(new_song);
496 return mpd_connection_clear_error(connection) ||
497 mpdclient_handle_error(c);
498 }
500 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
501 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
502 /* the cheap route: match on the new playlist length
503 and its version, we can keep our local playlist
504 copy in sync */
505 c->playlist.version = mpd_status_get_queue_version(status);
507 /* the song we just received has the correct id;
508 append it to the local playlist */
509 playlist_append(&c->playlist, new_song);
510 }
512 mpd_song_free(new_song);
514 return true;
515 }
517 bool
518 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
519 {
520 struct mpd_connection *connection = mpdclient_get_connection(c);
522 if (connection == NULL || c->status == NULL)
523 return false;
525 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
526 return false;
528 const struct mpd_song *song = playlist_get(&c->playlist, idx);
530 /* send the delete command to mpd; at the same time, get the
531 new status (to verify the playlist id) */
533 if (!mpd_command_list_begin(connection, false) ||
534 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
535 !mpd_send_status(connection) ||
536 !mpd_command_list_end(connection))
537 return mpdclient_handle_error(c);
539 c->events |= MPD_IDLE_QUEUE;
541 struct mpd_status *status = mpdclient_recv_status(c);
542 if (status == NULL)
543 return false;
545 if (!mpd_response_finish(connection))
546 return mpdclient_handle_error(c);
548 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
549 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
550 /* the cheap route: match on the new playlist length
551 and its version, we can keep our local playlist
552 copy in sync */
553 c->playlist.version = mpd_status_get_queue_version(status);
555 /* remove the song from the local playlist */
556 playlist_remove(&c->playlist, idx);
558 /* remove references to the song */
559 if (c->song == song)
560 c->song = NULL;
561 }
563 return true;
564 }
566 bool
567 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
568 {
569 if (end == start + 1)
570 /* if that's not really a range, we choose to use the
571 safer "deleteid" version */
572 return mpdclient_cmd_delete(c, start);
574 struct mpd_connection *connection = mpdclient_get_connection(c);
575 if (connection == NULL)
576 return false;
578 /* send the delete command to mpd; at the same time, get the
579 new status (to verify the playlist id) */
581 if (!mpd_command_list_begin(connection, false) ||
582 !mpd_send_delete_range(connection, start, end) ||
583 !mpd_send_status(connection) ||
584 !mpd_command_list_end(connection))
585 return mpdclient_handle_error(c);
587 c->events |= MPD_IDLE_QUEUE;
589 struct mpd_status *status = mpdclient_recv_status(c);
590 if (status == NULL)
591 return false;
593 if (!mpd_response_finish(connection))
594 return mpdclient_handle_error(c);
596 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
597 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
598 /* the cheap route: match on the new playlist length
599 and its version, we can keep our local playlist
600 copy in sync */
601 c->playlist.version = mpd_status_get_queue_version(status);
603 /* remove the song from the local playlist */
604 while (end > start) {
605 --end;
607 /* remove references to the song */
608 if (c->song == playlist_get(&c->playlist, end))
609 c->song = NULL;
611 playlist_remove(&c->playlist, end);
612 }
613 }
615 return true;
616 }
618 bool
619 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
620 {
621 if (dest_pos == src_pos)
622 return true;
624 struct mpd_connection *connection = mpdclient_get_connection(c);
625 if (connection == NULL)
626 return false;
628 /* send the "move" command to MPD; at the same time, get the
629 new status (to verify the playlist id) */
631 if (!mpd_command_list_begin(connection, false) ||
632 !mpd_send_move(connection, src_pos, dest_pos) ||
633 !mpd_send_status(connection) ||
634 !mpd_command_list_end(connection))
635 return mpdclient_handle_error(c);
637 c->events |= MPD_IDLE_QUEUE;
639 struct mpd_status *status = mpdclient_recv_status(c);
640 if (status == NULL)
641 return false;
643 if (!mpd_response_finish(connection))
644 return mpdclient_handle_error(c);
646 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
647 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
648 /* the cheap route: match on the new playlist length
649 and its version, we can keep our local playlist
650 copy in sync */
651 c->playlist.version = mpd_status_get_queue_version(status);
653 /* swap songs in the local playlist */
654 playlist_move(&c->playlist, dest_pos, src_pos);
655 }
657 return true;
658 }
660 /* The client-to-client protocol (MPD 0.17.0) */
662 bool
663 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
664 {
665 struct mpd_connection *connection = mpdclient_get_connection(c);
667 if (connection == NULL)
668 return false;
670 if (!mpd_send_subscribe(connection, channel))
671 return mpdclient_handle_error(c);
673 return mpdclient_finish_command(c);
674 }
676 bool
677 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
678 {
679 struct mpd_connection *connection = mpdclient_get_connection(c);
680 if (connection == NULL)
681 return false;
683 if (!mpd_send_unsubscribe(connection, channel))
684 return mpdclient_handle_error(c);
686 return mpdclient_finish_command(c);
687 }
689 bool
690 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
691 const char *text)
692 {
693 struct mpd_connection *connection = mpdclient_get_connection(c);
694 if (connection == NULL)
695 return false;
697 if (!mpd_send_send_message(connection, channel, text))
698 return mpdclient_handle_error(c);
700 return mpdclient_finish_command(c);
701 }
703 bool
704 mpdclient_send_read_messages(struct mpdclient *c)
705 {
706 struct mpd_connection *connection = mpdclient_get_connection(c);
707 if (connection == NULL)
708 return false;
710 return mpd_send_read_messages(connection)?
711 true : mpdclient_handle_error(c);
712 }
714 struct mpd_message *
715 mpdclient_recv_message(struct mpdclient *c)
716 {
717 struct mpd_connection *connection = mpdclient_get_connection(c);
718 if (connection == NULL)
719 return false;
721 struct mpd_message *message = mpd_recv_message(connection);
722 if (message == NULL &&
723 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
724 mpdclient_handle_error(c);
726 return message;
727 }
729 /****************************************************************************/
730 /*** Playlist management functions ******************************************/
731 /****************************************************************************/
733 /* update playlist */
734 bool
735 mpdclient_playlist_update(struct mpdclient *c)
736 {
737 struct mpd_connection *connection = mpdclient_get_connection(c);
738 if (connection == NULL)
739 return false;
741 playlist_clear(&c->playlist);
743 mpd_send_list_queue_meta(connection);
745 struct mpd_entity *entity;
746 while ((entity = mpd_recv_entity(connection))) {
747 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
748 playlist_append(&c->playlist, mpd_entity_get_song(entity));
750 mpd_entity_free(entity);
751 }
753 c->playlist.version = mpd_status_get_queue_version(c->status);
754 c->song = NULL;
756 return mpdclient_finish_command(c);
757 }
759 /* update playlist (plchanges) */
760 bool
761 mpdclient_playlist_update_changes(struct mpdclient *c)
762 {
763 struct mpd_connection *connection = mpdclient_get_connection(c);
765 if (connection == NULL)
766 return false;
768 mpd_send_queue_changes_meta(connection, c->playlist.version);
770 struct mpd_song *song;
771 while ((song = mpd_recv_song(connection)) != NULL) {
772 int pos = mpd_song_get_pos(song);
774 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
775 /* update song */
776 playlist_replace(&c->playlist, pos, song);
777 } else {
778 /* add a new song */
779 playlist_append(&c->playlist, song);
780 }
782 mpd_song_free(song);
783 }
785 /* remove trailing songs */
787 unsigned length = mpd_status_get_queue_length(c->status);
788 while (length < c->playlist.list->len) {
789 guint pos = c->playlist.list->len - 1;
791 /* Remove the last playlist entry */
792 playlist_remove(&c->playlist, pos);
793 }
795 c->song = NULL;
796 c->playlist.version = mpd_status_get_queue_version(c->status);
798 return mpdclient_finish_command(c);
799 }
802 /****************************************************************************/
803 /*** Filelist functions *****************************************************/
804 /****************************************************************************/
806 bool
807 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
808 {
809 struct mpd_connection *connection = mpdclient_get_connection(c);
810 if (connection == NULL)
811 return false;
813 if (filelist_is_empty(fl))
814 return true;
816 mpd_command_list_begin(connection, false);
818 for (unsigned i = 0; i < filelist_length(fl); ++i) {
819 struct filelist_entry *entry = filelist_get(fl, i);
820 struct mpd_entity *entity = entry->entity;
822 if (entity != NULL &&
823 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
824 const struct mpd_song *song =
825 mpd_entity_get_song(entity);
827 mpd_send_add(connection, mpd_song_get_uri(song));
828 }
829 }
831 mpd_command_list_end(connection);
832 return mpdclient_finish_command(c);
833 }