1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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 "filelist.h"
22 #include "screen_client.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 #define BUFSIZE 1024
38 /* sort by list-format */
39 gint
40 compare_filelistentry_format(gconstpointer filelist_entry1,
41 gconstpointer filelist_entry2)
42 {
43 const struct mpd_entity *e1, *e2;
44 char key1[BUFSIZE], key2[BUFSIZE];
45 int n = 0;
47 e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
48 e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
50 if (e1 && e2 &&
51 mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
52 mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
53 strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
54 strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
55 n = strcmp(key1,key2);
56 }
58 return n;
59 }
62 /****************************************************************************/
63 /*** mpdclient functions ****************************************************/
64 /****************************************************************************/
66 bool
67 mpdclient_handle_error(struct mpdclient *c)
68 {
69 enum mpd_error error = mpd_connection_get_error(c->connection);
71 assert(error != MPD_ERROR_SUCCESS);
73 if (error == MPD_ERROR_SERVER &&
74 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
75 screen_auth(c))
76 return true;
78 mpdclient_ui_error(mpd_connection_get_error_message(c->connection));
80 if (!mpd_connection_clear_error(c->connection))
81 mpdclient_disconnect(c);
83 return false;
84 }
86 struct mpdclient *
87 mpdclient_new(void)
88 {
89 struct mpdclient *c;
91 c = g_new0(struct mpdclient, 1);
92 playlist_init(&c->playlist);
93 c->volume = -1;
94 c->events = 0;
96 return c;
97 }
99 void
100 mpdclient_free(struct mpdclient *c)
101 {
102 mpdclient_disconnect(c);
104 mpdclient_playlist_free(&c->playlist);
106 g_free(c);
107 }
109 void
110 mpdclient_disconnect(struct mpdclient *c)
111 {
112 if (c->source != NULL) {
113 mpd_glib_free(c->source);
114 c->source = NULL;
115 c->idle = false;
116 }
118 if (c->connection) {
119 mpd_connection_free(c->connection);
120 ++c->connection_id;
121 }
122 c->connection = NULL;
124 if (c->status)
125 mpd_status_free(c->status);
126 c->status = NULL;
128 playlist_clear(&c->playlist);
130 if (c->song)
131 c->song = NULL;
133 /* everything has changed after a disconnect */
134 c->events |= MPD_IDLE_ALL;
135 }
137 bool
138 mpdclient_connect(struct mpdclient *c,
139 const gchar *host,
140 gint port,
141 unsigned timeout_ms,
142 const gchar *password)
143 {
144 /* close any open connection */
145 if (c->connection)
146 mpdclient_disconnect(c);
148 /* connect to MPD */
149 c->connection = mpd_connection_new(host, port, timeout_ms);
150 if (c->connection == NULL)
151 g_error("Out of memory");
153 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
154 mpdclient_handle_error(c);
155 mpdclient_disconnect(c);
156 return false;
157 }
159 /* send password */
160 if (password != NULL && !mpd_run_password(c->connection, password)) {
161 mpdclient_handle_error(c);
162 mpdclient_disconnect(c);
163 return false;
164 }
166 ++c->connection_id;
168 return true;
169 }
171 bool
172 mpdclient_update(struct mpdclient *c)
173 {
174 struct mpd_connection *connection = mpdclient_get_connection(c);
176 c->volume = -1;
178 if (connection == NULL)
179 return false;
181 /* always announce these options as long as we don't have
182 "idle" support */
183 if (c->source == NULL)
184 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
186 /* free the old status */
187 if (c->status)
188 mpd_status_free(c->status);
190 /* retrieve new status */
191 c->status = mpd_run_status(connection);
192 if (c->status == NULL)
193 return mpdclient_handle_error(c);
195 if (c->source == NULL &&
196 c->update_id != mpd_status_get_update_id(c->status)) {
197 c->events |= MPD_IDLE_UPDATE;
199 if (c->update_id > 0)
200 c->events |= MPD_IDLE_DATABASE;
201 }
203 c->update_id = mpd_status_get_update_id(c->status);
205 if (c->source == NULL &&
206 c->volume != mpd_status_get_volume(c->status))
207 c->events |= MPD_IDLE_MIXER;
209 c->volume = mpd_status_get_volume(c->status);
211 /* check if the playlist needs an update */
212 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
213 bool retval;
215 if (c->source == NULL)
216 c->events |= MPD_IDLE_QUEUE;
218 if (!playlist_is_empty(&c->playlist))
219 retval = mpdclient_playlist_update_changes(c);
220 else
221 retval = mpdclient_playlist_update(c);
222 if (!retval)
223 return false;
224 }
226 /* update the current song */
227 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
228 c->song = playlist_get_song(&c->playlist,
229 mpd_status_get_song_pos(c->status));
230 }
232 return true;
233 }
235 struct mpd_connection *
236 mpdclient_get_connection(struct mpdclient *c)
237 {
238 if (c->source != NULL && c->idle) {
239 c->idle = false;
240 mpd_glib_leave(c->source);
241 }
243 return c->connection;
244 }
246 void
247 mpdclient_put_connection(struct mpdclient *c)
248 {
249 assert(c->source == NULL || c->connection != NULL);
251 if (c->source != NULL && !c->idle) {
252 c->idle = mpd_glib_enter(c->source);
253 }
254 }
256 static struct mpd_status *
257 mpdclient_recv_status(struct mpdclient *c)
258 {
259 struct mpd_status *status;
261 assert(c->connection != NULL);
263 status = mpd_recv_status(c->connection);
264 if (status == NULL) {
265 mpdclient_handle_error(c);
266 return NULL;
267 }
269 if (c->status != NULL)
270 mpd_status_free(c->status);
271 return c->status = status;
272 }
274 /****************************************************************************/
275 /*** MPD Commands **********************************************************/
276 /****************************************************************************/
278 bool
279 mpdclient_cmd_crop(struct mpdclient *c)
280 {
281 struct mpd_connection *connection;
282 int length, current;
284 if (!mpdclient_is_playing(c))
285 return false;
287 length = mpd_status_get_queue_length(c->status);
288 current = mpd_status_get_song_pos(c->status);
289 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
290 return true;
292 connection = mpdclient_get_connection(c);
293 if (connection == NULL)
294 return false;
296 mpd_command_list_begin(connection, false);
298 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
299 if (current < length - 1)
300 mpd_send_delete_range(connection, current + 1, length);
301 if (current > 0)
302 mpd_send_delete_range(connection, 0, current);
303 } else
304 while (--length >= 0)
305 if (length != current)
306 mpd_send_delete(connection, length);
308 mpd_command_list_end(connection);
310 return mpdclient_finish_command(c);
311 }
313 bool
314 mpdclient_cmd_clear(struct mpdclient *c)
315 {
316 struct mpd_connection *connection = mpdclient_get_connection(c);
317 struct mpd_status *status;
319 if (connection == NULL)
320 return false;
322 /* send "clear" and "status" */
323 if (!mpd_command_list_begin(connection, false) ||
324 !mpd_send_clear(connection) ||
325 !mpd_send_status(connection) ||
326 !mpd_command_list_end(connection))
327 return mpdclient_handle_error(c);
329 /* receive the new status, store it in the mpdclient struct */
331 status = mpdclient_recv_status(c);
332 if (status == NULL)
333 return false;
335 if (!mpd_response_finish(connection))
336 return mpdclient_handle_error(c);
338 /* update mpdclient.playlist */
340 if (mpd_status_get_queue_length(status) == 0) {
341 /* after the "clear" command, the queue is really
342 empty - this means we can clear it locally,
343 reducing the UI latency */
344 playlist_clear(&c->playlist);
345 c->playlist.version = mpd_status_get_queue_version(status);
346 c->song = NULL;
347 }
349 c->events |= MPD_IDLE_QUEUE;
350 return true;
351 }
353 bool
354 mpdclient_cmd_volume(struct mpdclient *c, gint value)
355 {
356 struct mpd_connection *connection = mpdclient_get_connection(c);
357 if (connection == NULL)
358 return false;
360 mpd_send_set_volume(connection, value);
361 return mpdclient_finish_command(c);
362 }
364 bool
365 mpdclient_cmd_volume_up(struct mpdclient *c)
366 {
367 struct mpd_connection *connection = mpdclient_get_connection(c);
368 if (connection == NULL)
369 return false;
371 if (c->status == NULL ||
372 mpd_status_get_volume(c->status) == -1)
373 return true;
375 if (c->volume < 0)
376 c->volume = mpd_status_get_volume(c->status);
378 if (c->volume >= 100)
379 return true;
381 return mpdclient_cmd_volume(c, ++c->volume);
382 }
384 bool
385 mpdclient_cmd_volume_down(struct mpdclient *c)
386 {
387 struct mpd_connection *connection = mpdclient_get_connection(c);
388 if (connection == NULL)
389 return false;
391 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
392 return true;
394 if (c->volume < 0)
395 c->volume = mpd_status_get_volume(c->status);
397 if (c->volume <= 0)
398 return true;
400 return mpdclient_cmd_volume(c, --c->volume);
401 }
403 bool
404 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
405 {
406 struct mpd_connection *connection = mpdclient_get_connection(c);
407 if (connection == NULL)
408 return false;
410 return mpd_send_add(connection, path_utf8)?
411 mpdclient_finish_command(c) : false;
412 }
414 bool
415 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
416 {
417 struct mpd_connection *connection = mpdclient_get_connection(c);
418 struct mpd_status *status;
419 struct mpd_song *new_song;
421 assert(c != NULL);
422 assert(song != NULL);
424 if (connection == NULL || c->status == NULL)
425 return false;
427 /* send the add command to mpd; at the same time, get the new
428 status (to verify the new playlist id) and the last song
429 (we hope that's the song we just added) */
431 if (!mpd_command_list_begin(connection, true) ||
432 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
433 !mpd_send_status(connection) ||
434 !mpd_send_get_queue_song_pos(connection,
435 playlist_length(&c->playlist)) ||
436 !mpd_command_list_end(connection) ||
437 !mpd_response_next(connection))
438 return mpdclient_handle_error(c);
440 c->events |= MPD_IDLE_QUEUE;
442 status = mpdclient_recv_status(c);
443 if (status == NULL)
444 return false;
446 if (!mpd_response_next(connection))
447 return mpdclient_handle_error(c);
449 new_song = mpd_recv_song(connection);
450 if (!mpd_response_finish(connection) || new_song == NULL) {
451 if (new_song != NULL)
452 mpd_song_free(new_song);
454 return mpd_connection_clear_error(connection) ||
455 mpdclient_handle_error(c);
456 }
458 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
459 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
460 /* the cheap route: match on the new playlist length
461 and its version, we can keep our local playlist
462 copy in sync */
463 c->playlist.version = mpd_status_get_queue_version(status);
465 /* the song we just received has the correct id;
466 append it to the local playlist */
467 playlist_append(&c->playlist, new_song);
468 }
470 mpd_song_free(new_song);
472 return true;
473 }
475 bool
476 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
477 {
478 struct mpd_connection *connection = mpdclient_get_connection(c);
479 const struct mpd_song *song;
480 struct mpd_status *status;
482 if (connection == NULL || c->status == NULL)
483 return false;
485 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
486 return false;
488 song = playlist_get(&c->playlist, idx);
490 /* send the delete command to mpd; at the same time, get the
491 new status (to verify the playlist id) */
493 if (!mpd_command_list_begin(connection, false) ||
494 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
495 !mpd_send_status(connection) ||
496 !mpd_command_list_end(connection))
497 return mpdclient_handle_error(c);
499 c->events |= MPD_IDLE_QUEUE;
501 status = mpdclient_recv_status(c);
502 if (status == NULL)
503 return false;
505 if (!mpd_response_finish(connection))
506 return mpdclient_handle_error(c);
508 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
509 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
510 /* the cheap route: match on the new playlist length
511 and its version, we can keep our local playlist
512 copy in sync */
513 c->playlist.version = mpd_status_get_queue_version(status);
515 /* remove the song from the local playlist */
516 playlist_remove(&c->playlist, idx);
518 /* remove references to the song */
519 if (c->song == song)
520 c->song = NULL;
521 }
523 return true;
524 }
526 /**
527 * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
528 * It emulates the "delete range" command with a list of simple
529 * "delete" commands.
530 */
531 static bool
532 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
533 unsigned start, unsigned end)
534 {
535 struct mpd_connection *connection = mpdclient_get_connection(c);
536 if (connection == NULL)
537 return false;
539 if (!mpd_command_list_begin(connection, false))
540 return mpdclient_handle_error(c);
542 for (; start < end; --end)
543 mpd_send_delete(connection, start);
545 if (!mpd_command_list_end(connection) ||
546 !mpd_response_finish(connection))
547 return mpdclient_handle_error(c);
549 return true;
550 }
552 bool
553 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
554 {
555 struct mpd_connection *connection;
556 struct mpd_status *status;
558 if (end == start + 1)
559 /* if that's not really a range, we choose to use the
560 safer "deleteid" version */
561 return mpdclient_cmd_delete(c, start);
563 connection = mpdclient_get_connection(c);
564 if (connection == NULL)
565 return false;
567 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
568 return mpdclient_cmd_delete_range_fallback(c, start, end);
570 /* MPD 0.16 supports "delete" with a range argument */
572 /* send the delete command to mpd; at the same time, get the
573 new status (to verify the playlist id) */
575 if (!mpd_command_list_begin(connection, false) ||
576 !mpd_send_delete_range(connection, start, end) ||
577 !mpd_send_status(connection) ||
578 !mpd_command_list_end(connection))
579 return mpdclient_handle_error(c);
581 c->events |= MPD_IDLE_QUEUE;
583 status = mpdclient_recv_status(c);
584 if (status == NULL)
585 return false;
587 if (!mpd_response_finish(connection))
588 return mpdclient_handle_error(c);
590 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
591 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
592 /* the cheap route: match on the new playlist length
593 and its version, we can keep our local playlist
594 copy in sync */
595 c->playlist.version = mpd_status_get_queue_version(status);
597 /* remove the song from the local playlist */
598 while (end > start) {
599 --end;
601 /* remove references to the song */
602 if (c->song == playlist_get(&c->playlist, end))
603 c->song = NULL;
605 playlist_remove(&c->playlist, end);
606 }
607 }
609 return true;
610 }
612 bool
613 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
614 {
615 struct mpd_connection *connection;
616 struct mpd_status *status;
618 if (dest_pos == src_pos)
619 return true;
621 connection = mpdclient_get_connection(c);
622 if (connection == NULL)
623 return false;
625 /* send the "move" command to MPD; at the same time, get the
626 new status (to verify the playlist id) */
628 if (!mpd_command_list_begin(connection, false) ||
629 !mpd_send_move(connection, src_pos, dest_pos) ||
630 !mpd_send_status(connection) ||
631 !mpd_command_list_end(connection))
632 return mpdclient_handle_error(c);
634 c->events |= MPD_IDLE_QUEUE;
636 status = mpdclient_recv_status(c);
637 if (status == NULL)
638 return false;
640 if (!mpd_response_finish(connection))
641 return mpdclient_handle_error(c);
643 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
644 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
645 /* the cheap route: match on the new playlist length
646 and its version, we can keep our local playlist
647 copy in sync */
648 c->playlist.version = mpd_status_get_queue_version(status);
650 /* swap songs in the local playlist */
651 playlist_move(&c->playlist, dest_pos, src_pos);
652 }
654 return true;
655 }
657 #if LIBMPDCLIENT_CHECK_VERSION(2,5,0)
658 /* The client-to-client protocol (MPD 0.17.0) */
660 bool
661 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
662 {
663 struct mpd_connection *connection = mpdclient_get_connection(c);
665 if (connection == NULL)
666 return false;
668 if (!mpd_send_subscribe(connection, channel))
669 return mpdclient_handle_error(c);
671 return mpdclient_finish_command(c);
672 }
674 bool
675 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
676 {
677 struct mpd_connection *connection = mpdclient_get_connection(c);
678 if (connection == NULL)
679 return false;
681 if (!mpd_send_unsubscribe(connection, channel))
682 return mpdclient_handle_error(c);
684 return mpdclient_finish_command(c);
685 }
687 bool
688 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
689 const char *text)
690 {
691 struct mpd_connection *connection = mpdclient_get_connection(c);
692 if (connection == NULL)
693 return false;
695 if (!mpd_send_send_message(connection, channel, text))
696 return mpdclient_handle_error(c);
698 return mpdclient_finish_command(c);
699 }
701 bool
702 mpdclient_send_read_messages(struct mpdclient *c)
703 {
704 struct mpd_connection *connection = mpdclient_get_connection(c);
705 if (connection == NULL)
706 return false;
708 return mpd_send_read_messages(connection)?
709 true : mpdclient_handle_error(c);
710 }
712 struct mpd_message *
713 mpdclient_recv_message(struct mpdclient *c)
714 {
715 struct mpd_connection *connection = mpdclient_get_connection(c);
716 if (connection == NULL)
717 return false;
719 struct mpd_message *message = mpd_recv_message(connection);
720 if (message == NULL &&
721 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
722 mpdclient_handle_error(c);
724 return message;
725 }
726 #endif
728 /****************************************************************************/
729 /*** Playlist management functions ******************************************/
730 /****************************************************************************/
732 /* update playlist */
733 bool
734 mpdclient_playlist_update(struct mpdclient *c)
735 {
736 struct mpd_connection *connection = mpdclient_get_connection(c);
737 struct mpd_entity *entity;
739 if (connection == NULL)
740 return false;
742 playlist_clear(&c->playlist);
744 mpd_send_list_queue_meta(connection);
745 while ((entity = mpd_recv_entity(connection))) {
746 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
747 playlist_append(&c->playlist, mpd_entity_get_song(entity));
749 mpd_entity_free(entity);
750 }
752 c->playlist.version = mpd_status_get_queue_version(c->status);
753 c->song = NULL;
755 return mpdclient_finish_command(c);
756 }
758 /* update playlist (plchanges) */
759 bool
760 mpdclient_playlist_update_changes(struct mpdclient *c)
761 {
762 struct mpd_connection *connection = mpdclient_get_connection(c);
763 struct mpd_song *song;
764 guint length;
766 if (connection == NULL)
767 return false;
769 mpd_send_queue_changes_meta(connection, c->playlist.version);
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 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 guint i;
812 if (connection == NULL)
813 return false;
815 if (filelist_is_empty(fl))
816 return true;
818 mpd_command_list_begin(connection, false);
820 for (i = 0; i < filelist_length(fl); ++i) {
821 struct filelist_entry *entry = filelist_get(fl, i);
822 struct mpd_entity *entity = entry->entity;
824 if (entity != NULL &&
825 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
826 const struct mpd_song *song =
827 mpd_entity_get_song(entity);
829 mpd_send_add(connection, mpd_song_get_uri(song));
830 }
831 }
833 mpd_command_list_end(connection);
834 return mpdclient_finish_command(c);
835 }