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 (current < length - 1)
299 mpd_send_delete_range(connection, current + 1, length);
300 if (current > 0)
301 mpd_send_delete_range(connection, 0, current);
303 mpd_command_list_end(connection);
305 return mpdclient_finish_command(c);
306 }
308 bool
309 mpdclient_cmd_clear(struct mpdclient *c)
310 {
311 struct mpd_connection *connection = mpdclient_get_connection(c);
312 struct mpd_status *status;
314 if (connection == NULL)
315 return false;
317 /* send "clear" and "status" */
318 if (!mpd_command_list_begin(connection, false) ||
319 !mpd_send_clear(connection) ||
320 !mpd_send_status(connection) ||
321 !mpd_command_list_end(connection))
322 return mpdclient_handle_error(c);
324 /* receive the new status, store it in the mpdclient struct */
326 status = mpdclient_recv_status(c);
327 if (status == NULL)
328 return false;
330 if (!mpd_response_finish(connection))
331 return mpdclient_handle_error(c);
333 /* update mpdclient.playlist */
335 if (mpd_status_get_queue_length(status) == 0) {
336 /* after the "clear" command, the queue is really
337 empty - this means we can clear it locally,
338 reducing the UI latency */
339 playlist_clear(&c->playlist);
340 c->playlist.version = mpd_status_get_queue_version(status);
341 c->song = NULL;
342 }
344 c->events |= MPD_IDLE_QUEUE;
345 return true;
346 }
348 bool
349 mpdclient_cmd_volume(struct mpdclient *c, gint value)
350 {
351 struct mpd_connection *connection = mpdclient_get_connection(c);
352 if (connection == NULL)
353 return false;
355 mpd_send_set_volume(connection, value);
356 return mpdclient_finish_command(c);
357 }
359 bool
360 mpdclient_cmd_volume_up(struct mpdclient *c)
361 {
362 struct mpd_connection *connection = mpdclient_get_connection(c);
363 if (connection == NULL)
364 return false;
366 if (c->status == NULL ||
367 mpd_status_get_volume(c->status) == -1)
368 return true;
370 if (c->volume < 0)
371 c->volume = mpd_status_get_volume(c->status);
373 if (c->volume >= 100)
374 return true;
376 return mpdclient_cmd_volume(c, ++c->volume);
377 }
379 bool
380 mpdclient_cmd_volume_down(struct mpdclient *c)
381 {
382 struct mpd_connection *connection = mpdclient_get_connection(c);
383 if (connection == NULL)
384 return false;
386 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
387 return true;
389 if (c->volume < 0)
390 c->volume = mpd_status_get_volume(c->status);
392 if (c->volume <= 0)
393 return true;
395 return mpdclient_cmd_volume(c, --c->volume);
396 }
398 bool
399 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
400 {
401 struct mpd_connection *connection = mpdclient_get_connection(c);
402 if (connection == NULL)
403 return false;
405 return mpd_send_add(connection, path_utf8)?
406 mpdclient_finish_command(c) : false;
407 }
409 bool
410 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
411 {
412 struct mpd_connection *connection = mpdclient_get_connection(c);
413 struct mpd_status *status;
414 struct mpd_song *new_song;
416 assert(c != NULL);
417 assert(song != NULL);
419 if (connection == NULL || c->status == NULL)
420 return false;
422 /* send the add command to mpd; at the same time, get the new
423 status (to verify the new playlist id) and the last song
424 (we hope that's the song we just added) */
426 if (!mpd_command_list_begin(connection, true) ||
427 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
428 !mpd_send_status(connection) ||
429 !mpd_send_get_queue_song_pos(connection,
430 playlist_length(&c->playlist)) ||
431 !mpd_command_list_end(connection) ||
432 !mpd_response_next(connection))
433 return mpdclient_handle_error(c);
435 c->events |= MPD_IDLE_QUEUE;
437 status = mpdclient_recv_status(c);
438 if (status == NULL)
439 return false;
441 if (!mpd_response_next(connection))
442 return mpdclient_handle_error(c);
444 new_song = mpd_recv_song(connection);
445 if (!mpd_response_finish(connection) || new_song == NULL) {
446 if (new_song != NULL)
447 mpd_song_free(new_song);
449 return mpd_connection_clear_error(connection) ||
450 mpdclient_handle_error(c);
451 }
453 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
454 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
455 /* the cheap route: match on the new playlist length
456 and its version, we can keep our local playlist
457 copy in sync */
458 c->playlist.version = mpd_status_get_queue_version(status);
460 /* the song we just received has the correct id;
461 append it to the local playlist */
462 playlist_append(&c->playlist, new_song);
463 }
465 mpd_song_free(new_song);
467 return true;
468 }
470 bool
471 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
472 {
473 struct mpd_connection *connection = mpdclient_get_connection(c);
474 const struct mpd_song *song;
475 struct mpd_status *status;
477 if (connection == NULL || c->status == NULL)
478 return false;
480 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
481 return false;
483 song = playlist_get(&c->playlist, idx);
485 /* send the delete command to mpd; at the same time, get the
486 new status (to verify the playlist id) */
488 if (!mpd_command_list_begin(connection, false) ||
489 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
490 !mpd_send_status(connection) ||
491 !mpd_command_list_end(connection))
492 return mpdclient_handle_error(c);
494 c->events |= MPD_IDLE_QUEUE;
496 status = mpdclient_recv_status(c);
497 if (status == NULL)
498 return false;
500 if (!mpd_response_finish(connection))
501 return mpdclient_handle_error(c);
503 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
504 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
505 /* the cheap route: match on the new playlist length
506 and its version, we can keep our local playlist
507 copy in sync */
508 c->playlist.version = mpd_status_get_queue_version(status);
510 /* remove the song from the local playlist */
511 playlist_remove(&c->playlist, idx);
513 /* remove references to the song */
514 if (c->song == song)
515 c->song = NULL;
516 }
518 return true;
519 }
521 bool
522 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
523 {
524 struct mpd_connection *connection;
525 struct mpd_status *status;
527 if (end == start + 1)
528 /* if that's not really a range, we choose to use the
529 safer "deleteid" version */
530 return mpdclient_cmd_delete(c, start);
532 connection = mpdclient_get_connection(c);
533 if (connection == NULL)
534 return false;
536 /* send the delete command to mpd; at the same time, get the
537 new status (to verify the playlist id) */
539 if (!mpd_command_list_begin(connection, false) ||
540 !mpd_send_delete_range(connection, start, end) ||
541 !mpd_send_status(connection) ||
542 !mpd_command_list_end(connection))
543 return mpdclient_handle_error(c);
545 c->events |= MPD_IDLE_QUEUE;
547 status = mpdclient_recv_status(c);
548 if (status == NULL)
549 return false;
551 if (!mpd_response_finish(connection))
552 return mpdclient_handle_error(c);
554 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
555 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
556 /* the cheap route: match on the new playlist length
557 and its version, we can keep our local playlist
558 copy in sync */
559 c->playlist.version = mpd_status_get_queue_version(status);
561 /* remove the song from the local playlist */
562 while (end > start) {
563 --end;
565 /* remove references to the song */
566 if (c->song == playlist_get(&c->playlist, end))
567 c->song = NULL;
569 playlist_remove(&c->playlist, end);
570 }
571 }
573 return true;
574 }
576 bool
577 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
578 {
579 struct mpd_connection *connection;
580 struct mpd_status *status;
582 if (dest_pos == src_pos)
583 return true;
585 connection = mpdclient_get_connection(c);
586 if (connection == NULL)
587 return false;
589 /* send the "move" command to MPD; at the same time, get the
590 new status (to verify the playlist id) */
592 if (!mpd_command_list_begin(connection, false) ||
593 !mpd_send_move(connection, src_pos, dest_pos) ||
594 !mpd_send_status(connection) ||
595 !mpd_command_list_end(connection))
596 return mpdclient_handle_error(c);
598 c->events |= MPD_IDLE_QUEUE;
600 status = mpdclient_recv_status(c);
601 if (status == NULL)
602 return false;
604 if (!mpd_response_finish(connection))
605 return mpdclient_handle_error(c);
607 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
608 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
609 /* the cheap route: match on the new playlist length
610 and its version, we can keep our local playlist
611 copy in sync */
612 c->playlist.version = mpd_status_get_queue_version(status);
614 /* swap songs in the local playlist */
615 playlist_move(&c->playlist, dest_pos, src_pos);
616 }
618 return true;
619 }
621 #if LIBMPDCLIENT_CHECK_VERSION(2,5,0)
622 /* The client-to-client protocol (MPD 0.17.0) */
624 bool
625 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
626 {
627 struct mpd_connection *connection = mpdclient_get_connection(c);
629 if (connection == NULL)
630 return false;
632 if (!mpd_send_subscribe(connection, channel))
633 return mpdclient_handle_error(c);
635 return mpdclient_finish_command(c);
636 }
638 bool
639 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
640 {
641 struct mpd_connection *connection = mpdclient_get_connection(c);
642 if (connection == NULL)
643 return false;
645 if (!mpd_send_unsubscribe(connection, channel))
646 return mpdclient_handle_error(c);
648 return mpdclient_finish_command(c);
649 }
651 bool
652 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
653 const char *text)
654 {
655 struct mpd_connection *connection = mpdclient_get_connection(c);
656 if (connection == NULL)
657 return false;
659 if (!mpd_send_send_message(connection, channel, text))
660 return mpdclient_handle_error(c);
662 return mpdclient_finish_command(c);
663 }
665 bool
666 mpdclient_send_read_messages(struct mpdclient *c)
667 {
668 struct mpd_connection *connection = mpdclient_get_connection(c);
669 if (connection == NULL)
670 return false;
672 return mpd_send_read_messages(connection)?
673 true : mpdclient_handle_error(c);
674 }
676 struct mpd_message *
677 mpdclient_recv_message(struct mpdclient *c)
678 {
679 struct mpd_connection *connection = mpdclient_get_connection(c);
680 if (connection == NULL)
681 return false;
683 struct mpd_message *message = mpd_recv_message(connection);
684 if (message == NULL &&
685 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
686 mpdclient_handle_error(c);
688 return message;
689 }
690 #endif
692 /****************************************************************************/
693 /*** Playlist management functions ******************************************/
694 /****************************************************************************/
696 /* update playlist */
697 bool
698 mpdclient_playlist_update(struct mpdclient *c)
699 {
700 struct mpd_connection *connection = mpdclient_get_connection(c);
701 struct mpd_entity *entity;
703 if (connection == NULL)
704 return false;
706 playlist_clear(&c->playlist);
708 mpd_send_list_queue_meta(connection);
709 while ((entity = mpd_recv_entity(connection))) {
710 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
711 playlist_append(&c->playlist, mpd_entity_get_song(entity));
713 mpd_entity_free(entity);
714 }
716 c->playlist.version = mpd_status_get_queue_version(c->status);
717 c->song = NULL;
719 return mpdclient_finish_command(c);
720 }
722 /* update playlist (plchanges) */
723 bool
724 mpdclient_playlist_update_changes(struct mpdclient *c)
725 {
726 struct mpd_connection *connection = mpdclient_get_connection(c);
727 struct mpd_song *song;
728 guint length;
730 if (connection == NULL)
731 return false;
733 mpd_send_queue_changes_meta(connection, c->playlist.version);
735 while ((song = mpd_recv_song(connection)) != NULL) {
736 int pos = mpd_song_get_pos(song);
738 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
739 /* update song */
740 playlist_replace(&c->playlist, pos, song);
741 } else {
742 /* add a new song */
743 playlist_append(&c->playlist, song);
744 }
746 mpd_song_free(song);
747 }
749 /* remove trailing songs */
751 length = mpd_status_get_queue_length(c->status);
752 while (length < c->playlist.list->len) {
753 guint pos = c->playlist.list->len - 1;
755 /* Remove the last playlist entry */
756 playlist_remove(&c->playlist, pos);
757 }
759 c->song = NULL;
760 c->playlist.version = mpd_status_get_queue_version(c->status);
762 return mpdclient_finish_command(c);
763 }
766 /****************************************************************************/
767 /*** Filelist functions *****************************************************/
768 /****************************************************************************/
770 bool
771 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
772 {
773 struct mpd_connection *connection = mpdclient_get_connection(c);
774 guint i;
776 if (connection == NULL)
777 return false;
779 if (filelist_is_empty(fl))
780 return true;
782 mpd_command_list_begin(connection, false);
784 for (i = 0; i < filelist_length(fl); ++i) {
785 struct filelist_entry *entry = filelist_get(fl, i);
786 struct mpd_entity *entity = entry->entity;
788 if (entity != NULL &&
789 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
790 const struct mpd_song *song =
791 mpd_entity_get_song(entity);
793 mpd_send_add(connection, mpd_song_get_uri(song));
794 }
795 }
797 mpd_command_list_end(connection);
798 return mpdclient_finish_command(c);
799 }