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 "options.h"
25 #include "strfsong.h"
26 #include "utils.h"
27 #include "gidle.h"
29 #include <mpd/client.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <string.h>
36 /****************************************************************************/
37 /*** mpdclient functions ****************************************************/
38 /****************************************************************************/
40 bool
41 mpdclient_handle_error(struct mpdclient *c)
42 {
43 enum mpd_error error = mpd_connection_get_error(c->connection);
45 assert(error != MPD_ERROR_SUCCESS);
47 if (error == MPD_ERROR_SERVER &&
48 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
49 mpdclient_auth_callback(c))
50 return true;
52 mpdclient_error_callback(mpd_connection_get_error_message(c->connection));
54 if (!mpd_connection_clear_error(c->connection))
55 mpdclient_disconnect(c);
57 return false;
58 }
60 struct mpdclient *
61 mpdclient_new(void)
62 {
63 struct mpdclient *c = g_new0(struct mpdclient, 1);
64 playlist_init(&c->playlist);
65 c->volume = -1;
66 c->events = 0;
68 return c;
69 }
71 void
72 mpdclient_free(struct mpdclient *c)
73 {
74 mpdclient_disconnect(c);
76 mpdclient_playlist_free(&c->playlist);
78 g_free(c);
79 }
81 void
82 mpdclient_disconnect(struct mpdclient *c)
83 {
84 if (c->source != NULL) {
85 mpd_glib_free(c->source);
86 c->source = NULL;
87 c->idle = false;
88 }
90 if (c->connection) {
91 mpd_connection_free(c->connection);
92 ++c->connection_id;
93 }
94 c->connection = NULL;
96 if (c->status)
97 mpd_status_free(c->status);
98 c->status = NULL;
100 playlist_clear(&c->playlist);
102 if (c->song)
103 c->song = NULL;
105 /* everything has changed after a disconnect */
106 c->events |= MPD_IDLE_ALL;
107 }
109 bool
110 mpdclient_connect(struct mpdclient *c,
111 const gchar *host,
112 gint port,
113 unsigned timeout_ms,
114 const gchar *password)
115 {
116 /* close any open connection */
117 if (c->connection)
118 mpdclient_disconnect(c);
120 /* connect to MPD */
121 c->connection = mpd_connection_new(host, port, timeout_ms);
122 if (c->connection == NULL)
123 g_error("Out of memory");
125 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
126 mpdclient_handle_error(c);
127 mpdclient_disconnect(c);
128 return false;
129 }
131 /* send password */
132 if (password != NULL && !mpd_run_password(c->connection, password)) {
133 mpdclient_handle_error(c);
134 mpdclient_disconnect(c);
135 return false;
136 }
138 c->source = mpd_glib_new(c->connection,
139 mpdclient_idle_callback, c);
141 ++c->connection_id;
143 return true;
144 }
146 bool
147 mpdclient_update(struct mpdclient *c)
148 {
149 struct mpd_connection *connection = mpdclient_get_connection(c);
151 c->volume = -1;
153 if (connection == NULL)
154 return false;
156 /* free the old status */
157 if (c->status)
158 mpd_status_free(c->status);
160 /* retrieve new status */
161 c->status = mpd_run_status(connection);
162 if (c->status == NULL)
163 return mpdclient_handle_error(c);
165 c->update_id = mpd_status_get_update_id(c->status);
167 c->volume = mpd_status_get_volume(c->status);
169 /* check if the playlist needs an update */
170 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
171 bool retval;
173 if (!playlist_is_empty(&c->playlist))
174 retval = mpdclient_playlist_update_changes(c);
175 else
176 retval = mpdclient_playlist_update(c);
177 if (!retval)
178 return false;
179 }
181 /* update the current song */
182 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
183 c->song = playlist_get_song(&c->playlist,
184 mpd_status_get_song_pos(c->status));
185 }
187 return true;
188 }
190 struct mpd_connection *
191 mpdclient_get_connection(struct mpdclient *c)
192 {
193 if (c->source != NULL && c->idle) {
194 c->idle = false;
195 mpd_glib_leave(c->source);
196 }
198 return c->connection;
199 }
201 void
202 mpdclient_put_connection(struct mpdclient *c)
203 {
204 assert(c->source == NULL || c->connection != NULL);
206 if (c->source != NULL && !c->idle) {
207 c->idle = mpd_glib_enter(c->source);
208 }
209 }
211 static struct mpd_status *
212 mpdclient_recv_status(struct mpdclient *c)
213 {
214 assert(c->connection != NULL);
216 struct mpd_status *status = mpd_recv_status(c->connection);
217 if (status == NULL) {
218 mpdclient_handle_error(c);
219 return NULL;
220 }
222 if (c->status != NULL)
223 mpd_status_free(c->status);
224 return c->status = status;
225 }
227 /****************************************************************************/
228 /*** MPD Commands **********************************************************/
229 /****************************************************************************/
231 bool
232 mpdclient_cmd_crop(struct mpdclient *c)
233 {
234 if (!mpdclient_is_playing(c))
235 return false;
237 int length = mpd_status_get_queue_length(c->status);
238 int current = mpd_status_get_song_pos(c->status);
239 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
240 return true;
242 struct mpd_connection *connection = mpdclient_get_connection(c);
243 if (connection == NULL)
244 return false;
246 mpd_command_list_begin(connection, false);
248 if (current < length - 1)
249 mpd_send_delete_range(connection, current + 1, length);
250 if (current > 0)
251 mpd_send_delete_range(connection, 0, current);
253 mpd_command_list_end(connection);
255 return mpdclient_finish_command(c);
256 }
258 bool
259 mpdclient_cmd_clear(struct mpdclient *c)
260 {
261 struct mpd_connection *connection = mpdclient_get_connection(c);
262 if (connection == NULL)
263 return false;
265 /* send "clear" and "status" */
266 if (!mpd_command_list_begin(connection, false) ||
267 !mpd_send_clear(connection) ||
268 !mpd_send_status(connection) ||
269 !mpd_command_list_end(connection))
270 return mpdclient_handle_error(c);
272 /* receive the new status, store it in the mpdclient struct */
274 struct mpd_status *status = mpdclient_recv_status(c);
275 if (status == NULL)
276 return false;
278 if (!mpd_response_finish(connection))
279 return mpdclient_handle_error(c);
281 /* update mpdclient.playlist */
283 if (mpd_status_get_queue_length(status) == 0) {
284 /* after the "clear" command, the queue is really
285 empty - this means we can clear it locally,
286 reducing the UI latency */
287 playlist_clear(&c->playlist);
288 c->playlist.version = mpd_status_get_queue_version(status);
289 c->song = NULL;
290 }
292 c->events |= MPD_IDLE_QUEUE;
293 return true;
294 }
296 bool
297 mpdclient_cmd_volume(struct mpdclient *c, gint value)
298 {
299 struct mpd_connection *connection = mpdclient_get_connection(c);
300 if (connection == NULL)
301 return false;
303 mpd_send_set_volume(connection, value);
304 return mpdclient_finish_command(c);
305 }
307 bool
308 mpdclient_cmd_volume_up(struct mpdclient *c)
309 {
310 struct mpd_connection *connection = mpdclient_get_connection(c);
311 if (connection == NULL)
312 return false;
314 if (c->status == NULL ||
315 mpd_status_get_volume(c->status) == -1)
316 return true;
318 if (c->volume < 0)
319 c->volume = mpd_status_get_volume(c->status);
321 if (c->volume >= 100)
322 return true;
324 return mpdclient_cmd_volume(c, ++c->volume);
325 }
327 bool
328 mpdclient_cmd_volume_down(struct mpdclient *c)
329 {
330 struct mpd_connection *connection = mpdclient_get_connection(c);
331 if (connection == NULL)
332 return false;
334 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
335 return true;
337 if (c->volume < 0)
338 c->volume = mpd_status_get_volume(c->status);
340 if (c->volume <= 0)
341 return true;
343 return mpdclient_cmd_volume(c, --c->volume);
344 }
346 bool
347 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
348 {
349 struct mpd_connection *connection = mpdclient_get_connection(c);
350 if (connection == NULL)
351 return false;
353 return mpd_send_add(connection, path_utf8)?
354 mpdclient_finish_command(c) : false;
355 }
357 bool
358 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
359 {
360 assert(c != NULL);
361 assert(song != NULL);
363 struct mpd_connection *connection = mpdclient_get_connection(c);
364 if (connection == NULL || c->status == NULL)
365 return false;
367 /* send the add command to mpd; at the same time, get the new
368 status (to verify the new playlist id) and the last song
369 (we hope that's the song we just added) */
371 if (!mpd_command_list_begin(connection, true) ||
372 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
373 !mpd_send_status(connection) ||
374 !mpd_send_get_queue_song_pos(connection,
375 playlist_length(&c->playlist)) ||
376 !mpd_command_list_end(connection) ||
377 !mpd_response_next(connection))
378 return mpdclient_handle_error(c);
380 c->events |= MPD_IDLE_QUEUE;
382 struct mpd_status *status = mpdclient_recv_status(c);
383 if (status == NULL)
384 return false;
386 if (!mpd_response_next(connection))
387 return mpdclient_handle_error(c);
389 struct mpd_song *new_song = mpd_recv_song(connection);
390 if (!mpd_response_finish(connection) || new_song == NULL) {
391 if (new_song != NULL)
392 mpd_song_free(new_song);
394 return mpd_connection_clear_error(connection) ||
395 mpdclient_handle_error(c);
396 }
398 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
399 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
400 /* the cheap route: match on the new playlist length
401 and its version, we can keep our local playlist
402 copy in sync */
403 c->playlist.version = mpd_status_get_queue_version(status);
405 /* the song we just received has the correct id;
406 append it to the local playlist */
407 playlist_append(&c->playlist, new_song);
408 }
410 mpd_song_free(new_song);
412 return true;
413 }
415 bool
416 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
417 {
418 struct mpd_connection *connection = mpdclient_get_connection(c);
420 if (connection == NULL || c->status == NULL)
421 return false;
423 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
424 return false;
426 const struct mpd_song *song = playlist_get(&c->playlist, idx);
428 /* send the delete command to mpd; at the same time, get the
429 new status (to verify the playlist id) */
431 if (!mpd_command_list_begin(connection, false) ||
432 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
433 !mpd_send_status(connection) ||
434 !mpd_command_list_end(connection))
435 return mpdclient_handle_error(c);
437 c->events |= MPD_IDLE_QUEUE;
439 struct mpd_status *status = mpdclient_recv_status(c);
440 if (status == NULL)
441 return false;
443 if (!mpd_response_finish(connection))
444 return mpdclient_handle_error(c);
446 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
447 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
448 /* the cheap route: match on the new playlist length
449 and its version, we can keep our local playlist
450 copy in sync */
451 c->playlist.version = mpd_status_get_queue_version(status);
453 /* remove the song from the local playlist */
454 playlist_remove(&c->playlist, idx);
456 /* remove references to the song */
457 if (c->song == song)
458 c->song = NULL;
459 }
461 return true;
462 }
464 bool
465 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
466 {
467 if (end == start + 1)
468 /* if that's not really a range, we choose to use the
469 safer "deleteid" version */
470 return mpdclient_cmd_delete(c, start);
472 struct mpd_connection *connection = mpdclient_get_connection(c);
473 if (connection == NULL)
474 return false;
476 /* send the delete command to mpd; at the same time, get the
477 new status (to verify the playlist id) */
479 if (!mpd_command_list_begin(connection, false) ||
480 !mpd_send_delete_range(connection, start, end) ||
481 !mpd_send_status(connection) ||
482 !mpd_command_list_end(connection))
483 return mpdclient_handle_error(c);
485 c->events |= MPD_IDLE_QUEUE;
487 struct mpd_status *status = mpdclient_recv_status(c);
488 if (status == NULL)
489 return false;
491 if (!mpd_response_finish(connection))
492 return mpdclient_handle_error(c);
494 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
495 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
496 /* the cheap route: match on the new playlist length
497 and its version, we can keep our local playlist
498 copy in sync */
499 c->playlist.version = mpd_status_get_queue_version(status);
501 /* remove the song from the local playlist */
502 while (end > start) {
503 --end;
505 /* remove references to the song */
506 if (c->song == playlist_get(&c->playlist, end))
507 c->song = NULL;
509 playlist_remove(&c->playlist, end);
510 }
511 }
513 return true;
514 }
516 bool
517 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
518 {
519 if (dest_pos == src_pos)
520 return true;
522 struct mpd_connection *connection = mpdclient_get_connection(c);
523 if (connection == NULL)
524 return false;
526 /* send the "move" 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_move(connection, src_pos, dest_pos) ||
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) &&
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 /* swap songs in the local playlist */
552 playlist_move(&c->playlist, dest_pos, src_pos);
553 }
555 return true;
556 }
558 /* The client-to-client protocol (MPD 0.17.0) */
560 bool
561 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
562 {
563 struct mpd_connection *connection = mpdclient_get_connection(c);
565 if (connection == NULL)
566 return false;
568 if (!mpd_send_subscribe(connection, channel))
569 return mpdclient_handle_error(c);
571 return mpdclient_finish_command(c);
572 }
574 bool
575 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
576 {
577 struct mpd_connection *connection = mpdclient_get_connection(c);
578 if (connection == NULL)
579 return false;
581 if (!mpd_send_unsubscribe(connection, channel))
582 return mpdclient_handle_error(c);
584 return mpdclient_finish_command(c);
585 }
587 bool
588 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
589 const char *text)
590 {
591 struct mpd_connection *connection = mpdclient_get_connection(c);
592 if (connection == NULL)
593 return false;
595 if (!mpd_send_send_message(connection, channel, text))
596 return mpdclient_handle_error(c);
598 return mpdclient_finish_command(c);
599 }
601 bool
602 mpdclient_send_read_messages(struct mpdclient *c)
603 {
604 struct mpd_connection *connection = mpdclient_get_connection(c);
605 if (connection == NULL)
606 return false;
608 return mpd_send_read_messages(connection)?
609 true : mpdclient_handle_error(c);
610 }
612 struct mpd_message *
613 mpdclient_recv_message(struct mpdclient *c)
614 {
615 struct mpd_connection *connection = mpdclient_get_connection(c);
616 if (connection == NULL)
617 return false;
619 struct mpd_message *message = mpd_recv_message(connection);
620 if (message == NULL &&
621 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
622 mpdclient_handle_error(c);
624 return message;
625 }
627 /****************************************************************************/
628 /*** Playlist management functions ******************************************/
629 /****************************************************************************/
631 /* update playlist */
632 bool
633 mpdclient_playlist_update(struct mpdclient *c)
634 {
635 struct mpd_connection *connection = mpdclient_get_connection(c);
636 if (connection == NULL)
637 return false;
639 playlist_clear(&c->playlist);
641 mpd_send_list_queue_meta(connection);
643 struct mpd_entity *entity;
644 while ((entity = mpd_recv_entity(connection))) {
645 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
646 playlist_append(&c->playlist, mpd_entity_get_song(entity));
648 mpd_entity_free(entity);
649 }
651 c->playlist.version = mpd_status_get_queue_version(c->status);
652 c->song = NULL;
654 return mpdclient_finish_command(c);
655 }
657 /* update playlist (plchanges) */
658 bool
659 mpdclient_playlist_update_changes(struct mpdclient *c)
660 {
661 struct mpd_connection *connection = mpdclient_get_connection(c);
663 if (connection == NULL)
664 return false;
666 mpd_send_queue_changes_meta(connection, c->playlist.version);
668 struct mpd_song *song;
669 while ((song = mpd_recv_song(connection)) != NULL) {
670 int pos = mpd_song_get_pos(song);
672 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
673 /* update song */
674 playlist_replace(&c->playlist, pos, song);
675 } else {
676 /* add a new song */
677 playlist_append(&c->playlist, song);
678 }
680 mpd_song_free(song);
681 }
683 /* remove trailing songs */
685 unsigned length = mpd_status_get_queue_length(c->status);
686 while (length < c->playlist.list->len) {
687 guint pos = c->playlist.list->len - 1;
689 /* Remove the last playlist entry */
690 playlist_remove(&c->playlist, pos);
691 }
693 c->song = NULL;
694 c->playlist.version = mpd_status_get_queue_version(c->status);
696 return mpdclient_finish_command(c);
697 }
700 /****************************************************************************/
701 /*** Filelist functions *****************************************************/
702 /****************************************************************************/
704 bool
705 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
706 {
707 struct mpd_connection *connection = mpdclient_get_connection(c);
708 if (connection == NULL)
709 return false;
711 if (filelist_is_empty(fl))
712 return true;
714 mpd_command_list_begin(connection, false);
716 for (unsigned i = 0; i < filelist_length(fl); ++i) {
717 struct filelist_entry *entry = filelist_get(fl, i);
718 struct mpd_entity *entity = entry->entity;
720 if (entity != NULL &&
721 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
722 const struct mpd_song *song =
723 mpd_entity_get_song(entity);
725 mpd_send_add(connection, mpd_song_get_uri(song));
726 }
727 }
729 mpd_command_list_end(connection);
730 return mpdclient_finish_command(c);
731 }