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 static bool
223 mpdclient_connected(struct mpdclient *c,
224 struct mpd_connection *connection)
225 {
226 c->connection = connection;
228 if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
229 mpdclient_invoke_error_callback1(c);
230 mpdclient_disconnect(c);
231 mpdclient_failed_callback();
232 return false;
233 }
235 /* send password */
236 if (c->password != NULL &&
237 !mpd_run_password(connection, c->password)) {
238 mpdclient_invoke_error_callback1(c);
239 mpdclient_disconnect(c);
240 mpdclient_failed_callback();
241 return false;
242 }
244 c->source = mpd_glib_new(connection,
245 mpdclient_gidle_callback, c);
246 mpdclient_schedule_enter_idle(c);
248 ++c->connection_id;
250 mpdclient_connected_callback();
251 return true;
252 }
254 bool
255 mpdclient_connect(struct mpdclient *c)
256 {
257 /* close any open connection */
258 mpdclient_disconnect(c);
260 /* connect to MPD */
261 struct mpd_connection *connection =
262 mpd_connection_new(c->host, c->port, c->timeout_ms);
263 if (connection == NULL)
264 g_error("Out of memory");
266 return mpdclient_connected(c, connection);
267 }
269 bool
270 mpdclient_update(struct mpdclient *c)
271 {
272 struct mpd_connection *connection = mpdclient_get_connection(c);
274 if (connection == NULL)
275 return false;
277 /* free the old status */
278 mpdclient_status_free(c);
280 /* retrieve new status */
281 c->status = mpd_run_status(connection);
282 if (c->status == NULL)
283 return mpdclient_handle_error(c);
285 c->volume = mpd_status_get_volume(c->status);
286 c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
288 /* check if the playlist needs an update */
289 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
290 bool retval;
292 if (!playlist_is_empty(&c->playlist))
293 retval = mpdclient_playlist_update_changes(c);
294 else
295 retval = mpdclient_playlist_update(c);
296 if (!retval)
297 return false;
298 }
300 /* update the current song */
301 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
302 c->song = playlist_get_song(&c->playlist,
303 mpd_status_get_song_pos(c->status));
304 }
306 return true;
307 }
309 struct mpd_connection *
310 mpdclient_get_connection(struct mpdclient *c)
311 {
312 if (c->source != NULL && c->idle) {
313 c->idle = false;
314 mpd_glib_leave(c->source);
316 mpdclient_schedule_enter_idle(c);
317 }
319 return c->connection;
320 }
322 static struct mpd_status *
323 mpdclient_recv_status(struct mpdclient *c)
324 {
325 assert(c->connection != NULL);
327 struct mpd_status *status = mpd_recv_status(c->connection);
328 if (status == NULL) {
329 mpdclient_handle_error(c);
330 return NULL;
331 }
333 if (c->status != NULL)
334 mpd_status_free(c->status);
335 return c->status = status;
336 }
338 /****************************************************************************/
339 /*** MPD Commands **********************************************************/
340 /****************************************************************************/
342 bool
343 mpdclient_cmd_crop(struct mpdclient *c)
344 {
345 if (!mpdclient_is_playing(c))
346 return false;
348 int length = mpd_status_get_queue_length(c->status);
349 int current = mpd_status_get_song_pos(c->status);
350 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
351 return true;
353 struct mpd_connection *connection = mpdclient_get_connection(c);
354 if (connection == NULL)
355 return false;
357 mpd_command_list_begin(connection, false);
359 if (current < length - 1)
360 mpd_send_delete_range(connection, current + 1, length);
361 if (current > 0)
362 mpd_send_delete_range(connection, 0, current);
364 mpd_command_list_end(connection);
366 return mpdclient_finish_command(c);
367 }
369 bool
370 mpdclient_cmd_clear(struct mpdclient *c)
371 {
372 struct mpd_connection *connection = mpdclient_get_connection(c);
373 if (connection == NULL)
374 return false;
376 /* send "clear" and "status" */
377 if (!mpd_command_list_begin(connection, false) ||
378 !mpd_send_clear(connection) ||
379 !mpd_send_status(connection) ||
380 !mpd_command_list_end(connection))
381 return mpdclient_handle_error(c);
383 /* receive the new status, store it in the mpdclient struct */
385 struct mpd_status *status = mpdclient_recv_status(c);
386 if (status == NULL)
387 return false;
389 if (!mpd_response_finish(connection))
390 return mpdclient_handle_error(c);
392 /* update mpdclient.playlist */
394 if (mpd_status_get_queue_length(status) == 0) {
395 /* after the "clear" command, the queue is really
396 empty - this means we can clear it locally,
397 reducing the UI latency */
398 playlist_clear(&c->playlist);
399 c->playlist.version = mpd_status_get_queue_version(status);
400 c->song = NULL;
401 }
403 c->events |= MPD_IDLE_QUEUE;
404 return true;
405 }
407 bool
408 mpdclient_cmd_volume(struct mpdclient *c, gint value)
409 {
410 struct mpd_connection *connection = mpdclient_get_connection(c);
411 if (connection == NULL)
412 return false;
414 mpd_send_set_volume(connection, value);
415 return mpdclient_finish_command(c);
416 }
418 bool
419 mpdclient_cmd_volume_up(struct mpdclient *c)
420 {
421 if (c->volume < 0 || c->volume >= 100)
422 return true;
424 struct mpd_connection *connection = mpdclient_get_connection(c);
425 if (connection == NULL)
426 return false;
428 return mpdclient_cmd_volume(c, ++c->volume);
429 }
431 bool
432 mpdclient_cmd_volume_down(struct mpdclient *c)
433 {
434 if (c->volume <= 0)
435 return true;
437 struct mpd_connection *connection = mpdclient_get_connection(c);
438 if (connection == NULL)
439 return false;
441 return mpdclient_cmd_volume(c, --c->volume);
442 }
444 bool
445 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
446 {
447 struct mpd_connection *connection = mpdclient_get_connection(c);
448 if (connection == NULL)
449 return false;
451 return mpd_send_add(connection, path_utf8)?
452 mpdclient_finish_command(c) : false;
453 }
455 bool
456 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
457 {
458 assert(c != NULL);
459 assert(song != NULL);
461 struct mpd_connection *connection = mpdclient_get_connection(c);
462 if (connection == NULL || c->status == NULL)
463 return false;
465 /* send the add command to mpd; at the same time, get the new
466 status (to verify the new playlist id) and the last song
467 (we hope that's the song we just added) */
469 if (!mpd_command_list_begin(connection, true) ||
470 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
471 !mpd_send_status(connection) ||
472 !mpd_send_get_queue_song_pos(connection,
473 playlist_length(&c->playlist)) ||
474 !mpd_command_list_end(connection) ||
475 !mpd_response_next(connection))
476 return mpdclient_handle_error(c);
478 c->events |= MPD_IDLE_QUEUE;
480 struct mpd_status *status = mpdclient_recv_status(c);
481 if (status == NULL)
482 return false;
484 if (!mpd_response_next(connection))
485 return mpdclient_handle_error(c);
487 struct mpd_song *new_song = mpd_recv_song(connection);
488 if (!mpd_response_finish(connection) || new_song == NULL) {
489 if (new_song != NULL)
490 mpd_song_free(new_song);
492 return mpd_connection_clear_error(connection) ||
493 mpdclient_handle_error(c);
494 }
496 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
497 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
498 /* the cheap route: match on the new playlist length
499 and its version, we can keep our local playlist
500 copy in sync */
501 c->playlist.version = mpd_status_get_queue_version(status);
503 /* the song we just received has the correct id;
504 append it to the local playlist */
505 playlist_append(&c->playlist, new_song);
506 }
508 mpd_song_free(new_song);
510 return true;
511 }
513 bool
514 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
515 {
516 struct mpd_connection *connection = mpdclient_get_connection(c);
518 if (connection == NULL || c->status == NULL)
519 return false;
521 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
522 return false;
524 const struct mpd_song *song = playlist_get(&c->playlist, idx);
526 /* send the delete command to mpd; at the same time, get the
527 new status (to verify the playlist id) */
529 if (!mpd_command_list_begin(connection, false) ||
530 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
531 !mpd_send_status(connection) ||
532 !mpd_command_list_end(connection))
533 return mpdclient_handle_error(c);
535 c->events |= MPD_IDLE_QUEUE;
537 struct mpd_status *status = mpdclient_recv_status(c);
538 if (status == NULL)
539 return false;
541 if (!mpd_response_finish(connection))
542 return mpdclient_handle_error(c);
544 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
545 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
546 /* the cheap route: match on the new playlist length
547 and its version, we can keep our local playlist
548 copy in sync */
549 c->playlist.version = mpd_status_get_queue_version(status);
551 /* remove the song from the local playlist */
552 playlist_remove(&c->playlist, idx);
554 /* remove references to the song */
555 if (c->song == song)
556 c->song = NULL;
557 }
559 return true;
560 }
562 bool
563 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
564 {
565 if (end == start + 1)
566 /* if that's not really a range, we choose to use the
567 safer "deleteid" version */
568 return mpdclient_cmd_delete(c, start);
570 struct mpd_connection *connection = mpdclient_get_connection(c);
571 if (connection == NULL)
572 return false;
574 /* send the delete command to mpd; at the same time, get the
575 new status (to verify the playlist id) */
577 if (!mpd_command_list_begin(connection, false) ||
578 !mpd_send_delete_range(connection, start, end) ||
579 !mpd_send_status(connection) ||
580 !mpd_command_list_end(connection))
581 return mpdclient_handle_error(c);
583 c->events |= MPD_IDLE_QUEUE;
585 struct mpd_status *status = mpdclient_recv_status(c);
586 if (status == NULL)
587 return false;
589 if (!mpd_response_finish(connection))
590 return mpdclient_handle_error(c);
592 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
593 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
594 /* the cheap route: match on the new playlist length
595 and its version, we can keep our local playlist
596 copy in sync */
597 c->playlist.version = mpd_status_get_queue_version(status);
599 /* remove the song from the local playlist */
600 while (end > start) {
601 --end;
603 /* remove references to the song */
604 if (c->song == playlist_get(&c->playlist, end))
605 c->song = NULL;
607 playlist_remove(&c->playlist, end);
608 }
609 }
611 return true;
612 }
614 bool
615 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
616 {
617 if (dest_pos == src_pos)
618 return true;
620 struct mpd_connection *connection = mpdclient_get_connection(c);
621 if (connection == NULL)
622 return false;
624 /* send the "move" command to MPD; at the same time, get the
625 new status (to verify the playlist id) */
627 if (!mpd_command_list_begin(connection, false) ||
628 !mpd_send_move(connection, src_pos, dest_pos) ||
629 !mpd_send_status(connection) ||
630 !mpd_command_list_end(connection))
631 return mpdclient_handle_error(c);
633 c->events |= MPD_IDLE_QUEUE;
635 struct mpd_status *status = mpdclient_recv_status(c);
636 if (status == NULL)
637 return false;
639 if (!mpd_response_finish(connection))
640 return mpdclient_handle_error(c);
642 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
643 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
644 /* the cheap route: match on the new playlist length
645 and its version, we can keep our local playlist
646 copy in sync */
647 c->playlist.version = mpd_status_get_queue_version(status);
649 /* swap songs in the local playlist */
650 playlist_move(&c->playlist, dest_pos, src_pos);
651 }
653 return true;
654 }
656 /* The client-to-client protocol (MPD 0.17.0) */
658 bool
659 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
660 {
661 struct mpd_connection *connection = mpdclient_get_connection(c);
663 if (connection == NULL)
664 return false;
666 if (!mpd_send_subscribe(connection, channel))
667 return mpdclient_handle_error(c);
669 return mpdclient_finish_command(c);
670 }
672 bool
673 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
674 {
675 struct mpd_connection *connection = mpdclient_get_connection(c);
676 if (connection == NULL)
677 return false;
679 if (!mpd_send_unsubscribe(connection, channel))
680 return mpdclient_handle_error(c);
682 return mpdclient_finish_command(c);
683 }
685 bool
686 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
687 const char *text)
688 {
689 struct mpd_connection *connection = mpdclient_get_connection(c);
690 if (connection == NULL)
691 return false;
693 if (!mpd_send_send_message(connection, channel, text))
694 return mpdclient_handle_error(c);
696 return mpdclient_finish_command(c);
697 }
699 bool
700 mpdclient_send_read_messages(struct mpdclient *c)
701 {
702 struct mpd_connection *connection = mpdclient_get_connection(c);
703 if (connection == NULL)
704 return false;
706 return mpd_send_read_messages(connection)?
707 true : mpdclient_handle_error(c);
708 }
710 struct mpd_message *
711 mpdclient_recv_message(struct mpdclient *c)
712 {
713 struct mpd_connection *connection = mpdclient_get_connection(c);
714 if (connection == NULL)
715 return false;
717 struct mpd_message *message = mpd_recv_message(connection);
718 if (message == NULL &&
719 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
720 mpdclient_handle_error(c);
722 return message;
723 }
725 /****************************************************************************/
726 /*** Playlist management functions ******************************************/
727 /****************************************************************************/
729 /* update playlist */
730 bool
731 mpdclient_playlist_update(struct mpdclient *c)
732 {
733 struct mpd_connection *connection = mpdclient_get_connection(c);
734 if (connection == NULL)
735 return false;
737 playlist_clear(&c->playlist);
739 mpd_send_list_queue_meta(connection);
741 struct mpd_entity *entity;
742 while ((entity = mpd_recv_entity(connection))) {
743 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
744 playlist_append(&c->playlist, mpd_entity_get_song(entity));
746 mpd_entity_free(entity);
747 }
749 c->playlist.version = mpd_status_get_queue_version(c->status);
750 c->song = NULL;
752 return mpdclient_finish_command(c);
753 }
755 /* update playlist (plchanges) */
756 bool
757 mpdclient_playlist_update_changes(struct mpdclient *c)
758 {
759 struct mpd_connection *connection = mpdclient_get_connection(c);
761 if (connection == NULL)
762 return false;
764 mpd_send_queue_changes_meta(connection, c->playlist.version);
766 struct mpd_song *song;
767 while ((song = mpd_recv_song(connection)) != NULL) {
768 int pos = mpd_song_get_pos(song);
770 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
771 /* update song */
772 playlist_replace(&c->playlist, pos, song);
773 } else {
774 /* add a new song */
775 playlist_append(&c->playlist, song);
776 }
778 mpd_song_free(song);
779 }
781 /* remove trailing songs */
783 unsigned length = mpd_status_get_queue_length(c->status);
784 while (length < c->playlist.list->len) {
785 guint pos = c->playlist.list->len - 1;
787 /* Remove the last playlist entry */
788 playlist_remove(&c->playlist, pos);
789 }
791 c->song = NULL;
792 c->playlist.version = mpd_status_get_queue_version(c->status);
794 return mpdclient_finish_command(c);
795 }
798 /****************************************************************************/
799 /*** Filelist functions *****************************************************/
800 /****************************************************************************/
802 bool
803 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
804 {
805 struct mpd_connection *connection = mpdclient_get_connection(c);
806 if (connection == NULL)
807 return false;
809 if (filelist_is_empty(fl))
810 return true;
812 mpd_command_list_begin(connection, false);
814 for (unsigned i = 0; i < filelist_length(fl); ++i) {
815 struct filelist_entry *entry = filelist_get(fl, i);
816 struct mpd_entity *entity = entry->entity;
818 if (entity != NULL &&
819 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
820 const struct mpd_song *song =
821 mpd_entity_get_song(entity);
823 mpd_send_add(connection, mpd_song_get_uri(song));
824 }
825 }
827 mpd_command_list_end(connection);
828 return mpdclient_finish_command(c);
829 }