9b896e2691b6613ffa42e680f17be09b8fa701d6
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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 static bool
87 mpdclient_finish_command(struct mpdclient *c)
88 {
89 return mpd_response_finish(c->connection)
90 ? true : mpdclient_handle_error(c);
91 }
93 struct mpdclient *
94 mpdclient_new(void)
95 {
96 struct mpdclient *c;
98 c = g_new0(struct mpdclient, 1);
99 playlist_init(&c->playlist);
100 c->volume = -1;
101 c->events = 0;
103 return c;
104 }
106 void
107 mpdclient_free(struct mpdclient *c)
108 {
109 mpdclient_disconnect(c);
111 mpdclient_playlist_free(&c->playlist);
113 g_free(c);
114 }
116 void
117 mpdclient_disconnect(struct mpdclient *c)
118 {
119 if (c->source != NULL) {
120 mpd_glib_free(c->source);
121 c->source = NULL;
122 c->idle = false;
123 }
125 if (c->connection)
126 mpd_connection_free(c->connection);
127 c->connection = NULL;
129 if (c->status)
130 mpd_status_free(c->status);
131 c->status = NULL;
133 playlist_clear(&c->playlist);
135 if (c->song)
136 c->song = NULL;
138 /* everything has changed after a disconnect */
139 c->events |= MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
140 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
141 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
142 }
144 bool
145 mpdclient_connect(struct mpdclient *c,
146 const gchar *host,
147 gint port,
148 gfloat _timeout,
149 const gchar *password)
150 {
151 /* close any open connection */
152 if( c->connection )
153 mpdclient_disconnect(c);
155 /* connect to MPD */
156 c->connection = mpd_connection_new(host, port, _timeout * 1000);
157 if (c->connection == NULL)
158 g_error("Out of memory");
160 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
161 mpdclient_handle_error(c);
162 mpdclient_disconnect(c);
163 return false;
164 }
166 /* send password */
167 if (password != NULL && !mpd_run_password(c->connection, password)) {
168 mpdclient_handle_error(c);
169 mpdclient_disconnect(c);
170 return false;
171 }
173 return true;
174 }
176 bool
177 mpdclient_update(struct mpdclient *c)
178 {
179 struct mpd_connection *connection = mpdclient_get_connection(c);
180 bool retval;
182 c->volume = -1;
184 if (connection == NULL)
185 return false;
187 /* always announce these options as long as we don't have
188 "idle" support */
189 if (c->source == NULL)
190 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
192 /* free the old status */
193 if (c->status)
194 mpd_status_free(c->status);
196 /* retrieve new status */
197 c->status = mpd_run_status(connection);
198 if (c->status == NULL)
199 return mpdclient_handle_error(c);
201 if (c->source == NULL &&
202 c->update_id != mpd_status_get_update_id(c->status)) {
203 c->events |= MPD_IDLE_UPDATE;
205 if (c->update_id > 0)
206 c->events |= MPD_IDLE_DATABASE;
207 }
209 c->update_id = mpd_status_get_update_id(c->status);
211 if (c->source == NULL &&
212 c->volume != mpd_status_get_volume(c->status))
213 c->events |= MPD_IDLE_MIXER;
215 c->volume = mpd_status_get_volume(c->status);
217 /* check if the playlist needs an update */
218 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
219 if (c->source == NULL)
220 c->events |= MPD_IDLE_PLAYLIST;
222 if (!playlist_is_empty(&c->playlist))
223 retval = mpdclient_playlist_update_changes(c);
224 else
225 retval = mpdclient_playlist_update(c);
226 } else
227 retval = true;
229 /* update the current song */
230 if (!c->song || mpd_status_get_song_id(c->status)) {
231 c->song = playlist_get_song(&c->playlist,
232 mpd_status_get_song_pos(c->status));
233 }
235 return retval;
236 }
238 struct mpd_connection *
239 mpdclient_get_connection(struct mpdclient *c)
240 {
241 if (c->source != NULL && c->idle) {
242 c->idle = false;
243 mpd_glib_leave(c->source);
244 }
246 return c->connection;
247 }
249 void
250 mpdclient_put_connection(struct mpdclient *c)
251 {
252 assert(c->source == NULL || c->connection != NULL);
254 if (c->source != NULL && !c->idle) {
255 c->idle = true;
256 mpd_glib_enter(c->source);
257 }
258 }
261 /****************************************************************************/
262 /*** MPD Commands **********************************************************/
263 /****************************************************************************/
265 bool
266 mpdclient_cmd_play(struct mpdclient *c, gint idx)
267 {
268 struct mpd_connection *connection = mpdclient_get_connection(c);
269 const struct mpd_song *song = playlist_get_song(&c->playlist, idx);
271 if (connection == NULL)
272 return false;
274 if (song)
275 mpd_send_play_id(connection, mpd_song_get_id(song));
276 else
277 mpd_send_play(connection);
279 return mpdclient_finish_command(c);
280 }
282 bool
283 mpdclient_cmd_crop(struct mpdclient *c)
284 {
285 struct mpd_connection *connection = mpdclient_get_connection(c);
286 struct mpd_status *status;
287 bool playing;
288 int length, current;
290 if (connection == NULL)
291 return false;
293 status = mpd_run_status(connection);
294 if (status == NULL)
295 return mpdclient_handle_error(c);
297 playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
298 mpd_status_get_state(status) == MPD_STATE_PAUSE;
299 length = mpd_status_get_queue_length(status);
300 current = mpd_status_get_song_pos(status);
302 mpd_status_free(status);
304 if (!playing || length < 2)
305 return true;
307 mpd_command_list_begin(connection, false);
309 while (--length >= 0)
310 if (length != current)
311 mpd_send_delete(connection, length);
313 mpd_command_list_end(connection);
315 return mpdclient_finish_command(c);
316 }
318 bool
319 mpdclient_cmd_clear(struct mpdclient *c)
320 {
321 struct mpd_connection *connection = mpdclient_get_connection(c);
322 bool retval;
324 if (connection == NULL)
325 return false;
327 mpd_send_clear(connection);
328 retval = mpdclient_finish_command(c);
330 if (retval)
331 c->events |= MPD_IDLE_PLAYLIST;
333 return retval;
334 }
336 bool
337 mpdclient_cmd_volume(struct mpdclient *c, gint value)
338 {
339 struct mpd_connection *connection = mpdclient_get_connection(c);
340 if (connection == NULL)
341 return false;
343 mpd_send_set_volume(connection, value);
344 return mpdclient_finish_command(c);
345 }
347 bool
348 mpdclient_cmd_volume_up(struct mpdclient *c)
349 {
350 struct mpd_connection *connection = mpdclient_get_connection(c);
351 if (connection == NULL)
352 return false;
354 if (c->status == NULL ||
355 mpd_status_get_volume(c->status) == -1)
356 return true;
358 if (c->volume < 0)
359 c->volume = mpd_status_get_volume(c->status);
361 if (c->volume >= 100)
362 return true;
364 return mpdclient_cmd_volume(c, ++c->volume);
365 }
367 bool
368 mpdclient_cmd_volume_down(struct mpdclient *c)
369 {
370 struct mpd_connection *connection = mpdclient_get_connection(c);
371 if (connection == NULL)
372 return false;
374 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
375 return true;
377 if (c->volume < 0)
378 c->volume = mpd_status_get_volume(c->status);
380 if (c->volume <= 0)
381 return true;
383 return mpdclient_cmd_volume(c, --c->volume);
384 }
386 bool
387 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
388 {
389 struct mpd_connection *connection = mpdclient_get_connection(c);
390 if (connection == NULL)
391 return false;
393 mpd_send_add(connection, path_utf8);
394 return mpdclient_finish_command(c);
395 }
397 bool
398 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
399 {
400 struct mpd_connection *connection = mpdclient_get_connection(c);
401 struct mpd_status *status;
402 struct mpd_song *new_song;
404 assert(c != NULL);
405 assert(song != NULL);
407 if (connection == NULL || c->status == NULL)
408 return false;
410 /* send the add command to mpd; at the same time, get the new
411 status (to verify the new playlist id) and the last song
412 (we hope that's the song we just added) */
414 if (!mpd_command_list_begin(connection, true) ||
415 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
416 !mpd_send_status(connection) ||
417 !mpd_send_get_queue_song_pos(connection,
418 playlist_length(&c->playlist)) ||
419 !mpd_command_list_end(connection) ||
420 !mpd_response_next(connection))
421 return mpdclient_handle_error(c);
423 c->events |= MPD_IDLE_PLAYLIST;
425 status = mpd_recv_status(connection);
426 if (status != NULL) {
427 if (c->status != NULL)
428 mpd_status_free(c->status);
429 c->status = status;
430 }
432 if (!mpd_response_next(connection))
433 return mpdclient_handle_error(c);
435 new_song = mpd_recv_song(connection);
436 if (!mpd_response_finish(connection) || new_song == NULL) {
437 if (new_song != NULL)
438 mpd_song_free(new_song);
440 return mpd_connection_clear_error(connection) ||
441 mpdclient_handle_error(c);
442 }
444 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
445 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
446 /* the cheap route: match on the new playlist length
447 and its version, we can keep our local playlist
448 copy in sync */
449 c->playlist.version = mpd_status_get_queue_version(status);
451 /* the song we just received has the correct id;
452 append it to the local playlist */
453 playlist_append(&c->playlist, new_song);
454 }
456 mpd_song_free(new_song);
458 return true;
459 }
461 bool
462 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
463 {
464 struct mpd_connection *connection = mpdclient_get_connection(c);
465 const struct mpd_song *song;
466 struct mpd_status *status;
468 if (connection == NULL || c->status == NULL)
469 return false;
471 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
472 return false;
474 song = playlist_get(&c->playlist, idx);
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_id(connection, mpd_song_get_id(song)) ||
481 !mpd_send_status(connection) ||
482 !mpd_command_list_end(connection))
483 return mpdclient_handle_error(c);
485 c->events |= MPD_IDLE_PLAYLIST;
487 status = mpd_recv_status(connection);
488 if (status != NULL) {
489 if (c->status != NULL)
490 mpd_status_free(c->status);
491 c->status = status;
492 }
494 if (!mpd_response_finish(connection))
495 return mpdclient_handle_error(c);
497 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
498 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
499 /* the cheap route: match on the new playlist length
500 and its version, we can keep our local playlist
501 copy in sync */
502 c->playlist.version = mpd_status_get_queue_version(status);
504 /* remove the song from the local playlist */
505 playlist_remove(&c->playlist, idx);
507 /* remove references to the song */
508 if (c->song == song)
509 c->song = NULL;
510 }
512 return true;
513 }
515 /**
516 * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
517 * It emulates the "delete range" command with a list of simple
518 * "delete" commands.
519 */
520 static bool
521 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
522 unsigned start, unsigned end)
523 {
524 struct mpd_connection *connection = mpdclient_get_connection(c);
525 if (connection == NULL)
526 return false;
528 if (!mpd_command_list_begin(connection, false))
529 return mpdclient_handle_error(c);
531 for (; start < end; --end)
532 mpd_send_delete(connection, start);
534 if (!mpd_command_list_end(connection) ||
535 !mpd_response_finish(connection))
536 return mpdclient_handle_error(c);
538 return true;
539 }
541 bool
542 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
543 {
544 struct mpd_connection *connection;
545 struct mpd_status *status;
547 connection = mpdclient_get_connection(c);
548 if (connection == NULL)
549 return false;
551 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
552 return mpdclient_cmd_delete_range_fallback(c, start, end);
554 /* MPD 0.16 supports "delete" with a range argument */
556 /* send the delete command to mpd; at the same time, get the
557 new status (to verify the playlist id) */
559 if (!mpd_command_list_begin(connection, false) ||
560 !mpd_send_delete_range(connection, start, end) ||
561 !mpd_send_status(connection) ||
562 !mpd_command_list_end(connection))
563 return mpdclient_handle_error(c);
565 c->events |= MPD_IDLE_PLAYLIST;
567 status = mpd_recv_status(connection);
568 if (status != NULL) {
569 if (c->status != NULL)
570 mpd_status_free(c->status);
571 c->status = status;
572 }
574 if (!mpd_response_finish(connection))
575 return mpdclient_handle_error(c);
577 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
578 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
579 /* the cheap route: match on the new playlist length
580 and its version, we can keep our local playlist
581 copy in sync */
582 c->playlist.version = mpd_status_get_queue_version(status);
584 /* remove the song from the local playlist */
585 while (end > start) {
586 --end;
588 /* remove references to the song */
589 if (c->song == playlist_get(&c->playlist, end))
590 c->song = NULL;
592 playlist_remove(&c->playlist, end);
593 }
594 }
596 return true;
597 }
599 bool
600 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
601 {
602 struct mpd_connection *connection = mpdclient_get_connection(c);
603 const struct mpd_song *song1, *song2;
604 struct mpd_status *status;
606 if (connection == NULL)
607 return false;
609 if (old_index == new_index || new_index < 0 ||
610 (guint)new_index >= c->playlist.list->len)
611 return false;
613 song1 = playlist_get(&c->playlist, old_index);
614 song2 = playlist_get(&c->playlist, new_index);
616 /* send the delete command to mpd; at the same time, get the
617 new status (to verify the playlist id) */
619 if (!mpd_command_list_begin(connection, false) ||
620 !mpd_send_swap_id(connection, mpd_song_get_id(song1),
621 mpd_song_get_id(song2)) ||
622 !mpd_send_status(connection) ||
623 !mpd_command_list_end(connection))
624 return mpdclient_handle_error(c);
626 c->events |= MPD_IDLE_PLAYLIST;
628 status = mpd_recv_status(connection);
629 if (status != NULL) {
630 if (c->status != NULL)
631 mpd_status_free(c->status);
632 c->status = status;
633 }
635 if (!mpd_response_finish(connection))
636 return mpdclient_handle_error(c);
638 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
639 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
640 /* the cheap route: match on the new playlist length
641 and its version, we can keep our local playlist
642 copy in sync */
643 c->playlist.version = mpd_status_get_queue_version(status);
645 /* swap songs in the local playlist */
646 playlist_swap(&c->playlist, old_index, new_index);
647 }
649 return true;
650 }
653 /****************************************************************************/
654 /*** Playlist management functions ******************************************/
655 /****************************************************************************/
657 /* update playlist */
658 bool
659 mpdclient_playlist_update(struct mpdclient *c)
660 {
661 struct mpd_connection *connection = mpdclient_get_connection(c);
662 struct mpd_entity *entity;
664 if (connection == NULL)
665 return false;
667 playlist_clear(&c->playlist);
669 mpd_send_list_queue_meta(connection);
670 while ((entity = mpd_recv_entity(connection))) {
671 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
672 playlist_append(&c->playlist, mpd_entity_get_song(entity));
674 mpd_entity_free(entity);
675 }
677 c->playlist.version = mpd_status_get_queue_version(c->status);
678 c->song = NULL;
680 return mpdclient_finish_command(c);
681 }
683 /* update playlist (plchanges) */
684 bool
685 mpdclient_playlist_update_changes(struct mpdclient *c)
686 {
687 struct mpd_connection *connection = mpdclient_get_connection(c);
688 struct mpd_song *song;
689 guint length;
691 if (connection == NULL)
692 return false;
694 mpd_send_queue_changes_meta(connection, c->playlist.version);
696 while ((song = mpd_recv_song(connection)) != NULL) {
697 int pos = mpd_song_get_pos(song);
699 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
700 /* update song */
701 playlist_replace(&c->playlist, pos, song);
702 } else {
703 /* add a new song */
704 playlist_append(&c->playlist, song);
705 }
707 mpd_song_free(song);
708 }
710 /* remove trailing songs */
712 length = mpd_status_get_queue_length(c->status);
713 while (length < c->playlist.list->len) {
714 guint pos = c->playlist.list->len - 1;
716 /* Remove the last playlist entry */
717 playlist_remove(&c->playlist, pos);
718 }
720 c->song = NULL;
721 c->playlist.version = mpd_status_get_queue_version(c->status);
723 return mpdclient_finish_command(c);
724 }
727 /****************************************************************************/
728 /*** Filelist functions *****************************************************/
729 /****************************************************************************/
731 bool
732 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
733 {
734 struct mpd_connection *connection = mpdclient_get_connection(c);
735 guint i;
737 if (connection == NULL)
738 return false;
740 if (filelist_is_empty(fl))
741 return true;
743 mpd_command_list_begin(connection, false);
745 for (i = 0; i < filelist_length(fl); ++i) {
746 struct filelist_entry *entry = filelist_get(fl, i);
747 struct mpd_entity *entity = entry->entity;
749 if (entity != NULL &&
750 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
751 const struct mpd_song *song =
752 mpd_entity_get_song(entity);
754 mpd_send_add(connection, mpd_song_get_uri(song));
755 }
756 }
758 mpd_command_list_end(connection);
759 return mpdclient_finish_command(c);
760 }
762 static GList *
763 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag)
764 {
765 GList *list = NULL;
766 struct mpd_pair *pair;
768 while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
769 list = g_list_append(list, g_strdup(pair->value));
770 mpd_return_pair(connection, pair);
771 }
773 return list;
774 }
776 GList *
777 mpdclient_get_artists(struct mpdclient *c)
778 {
779 struct mpd_connection *connection = mpdclient_get_connection(c);
780 GList *list;
782 if (connection == NULL)
783 return NULL;
785 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
786 mpd_search_commit(connection);
788 list = recv_tag_values(connection, MPD_TAG_ARTIST);
790 if (!mpdclient_finish_command(c))
791 return string_list_free(list);
793 return list;
794 }
796 GList *
797 mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8)
798 {
799 struct mpd_connection *connection = mpdclient_get_connection(c);
800 GList *list;
802 if (connection == NULL)
803 return NULL;
805 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
806 if (artist_utf8 != NULL)
807 mpd_search_add_tag_constraint(connection,
808 MPD_OPERATOR_DEFAULT,
809 MPD_TAG_ARTIST, artist_utf8);
810 mpd_search_commit(connection);
812 list = recv_tag_values(connection, MPD_TAG_ALBUM);
814 if (!mpdclient_finish_command(c))
815 return string_list_free(list);
817 return list;
818 }