9baf70bc7134fa8d2125f4ff463660052eb3ff82
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 =
44 ((const struct filelist_entry *)filelist_entry1)->entity;
45 const struct mpd_entity *e2 =
46 ((const struct filelist_entry *)filelist_entry2)->entity;
48 int n = 0;
49 if (e1 && e2 &&
50 mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
51 mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
52 char key1[BUFSIZE], key2[BUFSIZE];
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 = g_new0(struct mpdclient, 1);
90 playlist_init(&c->playlist);
91 c->volume = -1;
92 c->events = 0;
94 return c;
95 }
97 void
98 mpdclient_free(struct mpdclient *c)
99 {
100 mpdclient_disconnect(c);
102 mpdclient_playlist_free(&c->playlist);
104 g_free(c);
105 }
107 void
108 mpdclient_disconnect(struct mpdclient *c)
109 {
110 if (c->source != NULL) {
111 mpd_glib_free(c->source);
112 c->source = NULL;
113 c->idle = false;
114 }
116 if (c->connection) {
117 mpd_connection_free(c->connection);
118 ++c->connection_id;
119 }
120 c->connection = NULL;
122 if (c->status)
123 mpd_status_free(c->status);
124 c->status = NULL;
126 playlist_clear(&c->playlist);
128 if (c->song)
129 c->song = NULL;
131 /* everything has changed after a disconnect */
132 c->events |= MPD_IDLE_ALL;
133 }
135 bool
136 mpdclient_connect(struct mpdclient *c,
137 const gchar *host,
138 gint port,
139 unsigned timeout_ms,
140 const gchar *password)
141 {
142 /* close any open connection */
143 if (c->connection)
144 mpdclient_disconnect(c);
146 /* connect to MPD */
147 c->connection = mpd_connection_new(host, port, timeout_ms);
148 if (c->connection == NULL)
149 g_error("Out of memory");
151 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
152 mpdclient_handle_error(c);
153 mpdclient_disconnect(c);
154 return false;
155 }
157 /* send password */
158 if (password != NULL && !mpd_run_password(c->connection, password)) {
159 mpdclient_handle_error(c);
160 mpdclient_disconnect(c);
161 return false;
162 }
164 ++c->connection_id;
166 return true;
167 }
169 bool
170 mpdclient_update(struct mpdclient *c)
171 {
172 struct mpd_connection *connection = mpdclient_get_connection(c);
174 c->volume = -1;
176 if (connection == NULL)
177 return false;
179 /* always announce these options as long as we don't have
180 "idle" support */
181 if (c->source == NULL)
182 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
184 /* free the old status */
185 if (c->status)
186 mpd_status_free(c->status);
188 /* retrieve new status */
189 c->status = mpd_run_status(connection);
190 if (c->status == NULL)
191 return mpdclient_handle_error(c);
193 if (c->source == NULL &&
194 c->update_id != mpd_status_get_update_id(c->status)) {
195 c->events |= MPD_IDLE_UPDATE;
197 if (c->update_id > 0)
198 c->events |= MPD_IDLE_DATABASE;
199 }
201 c->update_id = mpd_status_get_update_id(c->status);
203 if (c->source == NULL &&
204 c->volume != mpd_status_get_volume(c->status))
205 c->events |= MPD_IDLE_MIXER;
207 c->volume = mpd_status_get_volume(c->status);
209 /* check if the playlist needs an update */
210 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
211 bool retval;
213 if (c->source == NULL)
214 c->events |= MPD_IDLE_QUEUE;
216 if (!playlist_is_empty(&c->playlist))
217 retval = mpdclient_playlist_update_changes(c);
218 else
219 retval = mpdclient_playlist_update(c);
220 if (!retval)
221 return false;
222 }
224 /* update the current song */
225 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
226 c->song = playlist_get_song(&c->playlist,
227 mpd_status_get_song_pos(c->status));
228 }
230 return true;
231 }
233 struct mpd_connection *
234 mpdclient_get_connection(struct mpdclient *c)
235 {
236 if (c->source != NULL && c->idle) {
237 c->idle = false;
238 mpd_glib_leave(c->source);
239 }
241 return c->connection;
242 }
244 void
245 mpdclient_put_connection(struct mpdclient *c)
246 {
247 assert(c->source == NULL || c->connection != NULL);
249 if (c->source != NULL && !c->idle) {
250 c->idle = mpd_glib_enter(c->source);
251 }
252 }
254 static struct mpd_status *
255 mpdclient_recv_status(struct mpdclient *c)
256 {
257 assert(c->connection != NULL);
259 struct mpd_status *status = mpd_recv_status(c->connection);
260 if (status == NULL) {
261 mpdclient_handle_error(c);
262 return NULL;
263 }
265 if (c->status != NULL)
266 mpd_status_free(c->status);
267 return c->status = status;
268 }
270 /****************************************************************************/
271 /*** MPD Commands **********************************************************/
272 /****************************************************************************/
274 bool
275 mpdclient_cmd_crop(struct mpdclient *c)
276 {
277 if (!mpdclient_is_playing(c))
278 return false;
280 int length = mpd_status_get_queue_length(c->status);
281 int current = mpd_status_get_song_pos(c->status);
282 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
283 return true;
285 struct mpd_connection *connection = mpdclient_get_connection(c);
286 if (connection == NULL)
287 return false;
289 mpd_command_list_begin(connection, false);
291 if (current < length - 1)
292 mpd_send_delete_range(connection, current + 1, length);
293 if (current > 0)
294 mpd_send_delete_range(connection, 0, current);
296 mpd_command_list_end(connection);
298 return mpdclient_finish_command(c);
299 }
301 bool
302 mpdclient_cmd_clear(struct mpdclient *c)
303 {
304 struct mpd_connection *connection = mpdclient_get_connection(c);
305 if (connection == NULL)
306 return false;
308 /* send "clear" and "status" */
309 if (!mpd_command_list_begin(connection, false) ||
310 !mpd_send_clear(connection) ||
311 !mpd_send_status(connection) ||
312 !mpd_command_list_end(connection))
313 return mpdclient_handle_error(c);
315 /* receive the new status, store it in the mpdclient struct */
317 struct mpd_status *status = mpdclient_recv_status(c);
318 if (status == NULL)
319 return false;
321 if (!mpd_response_finish(connection))
322 return mpdclient_handle_error(c);
324 /* update mpdclient.playlist */
326 if (mpd_status_get_queue_length(status) == 0) {
327 /* after the "clear" command, the queue is really
328 empty - this means we can clear it locally,
329 reducing the UI latency */
330 playlist_clear(&c->playlist);
331 c->playlist.version = mpd_status_get_queue_version(status);
332 c->song = NULL;
333 }
335 c->events |= MPD_IDLE_QUEUE;
336 return true;
337 }
339 bool
340 mpdclient_cmd_volume(struct mpdclient *c, gint value)
341 {
342 struct mpd_connection *connection = mpdclient_get_connection(c);
343 if (connection == NULL)
344 return false;
346 mpd_send_set_volume(connection, value);
347 return mpdclient_finish_command(c);
348 }
350 bool
351 mpdclient_cmd_volume_up(struct mpdclient *c)
352 {
353 struct mpd_connection *connection = mpdclient_get_connection(c);
354 if (connection == NULL)
355 return false;
357 if (c->status == NULL ||
358 mpd_status_get_volume(c->status) == -1)
359 return true;
361 if (c->volume < 0)
362 c->volume = mpd_status_get_volume(c->status);
364 if (c->volume >= 100)
365 return true;
367 return mpdclient_cmd_volume(c, ++c->volume);
368 }
370 bool
371 mpdclient_cmd_volume_down(struct mpdclient *c)
372 {
373 struct mpd_connection *connection = mpdclient_get_connection(c);
374 if (connection == NULL)
375 return false;
377 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
378 return true;
380 if (c->volume < 0)
381 c->volume = mpd_status_get_volume(c->status);
383 if (c->volume <= 0)
384 return true;
386 return mpdclient_cmd_volume(c, --c->volume);
387 }
389 bool
390 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
391 {
392 struct mpd_connection *connection = mpdclient_get_connection(c);
393 if (connection == NULL)
394 return false;
396 return mpd_send_add(connection, path_utf8)?
397 mpdclient_finish_command(c) : false;
398 }
400 bool
401 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
402 {
403 assert(c != NULL);
404 assert(song != NULL);
406 struct mpd_connection *connection = mpdclient_get_connection(c);
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_QUEUE;
425 struct mpd_status *status = mpdclient_recv_status(c);
426 if (status == NULL)
427 return false;
429 if (!mpd_response_next(connection))
430 return mpdclient_handle_error(c);
432 struct mpd_song *new_song = mpd_recv_song(connection);
433 if (!mpd_response_finish(connection) || new_song == NULL) {
434 if (new_song != NULL)
435 mpd_song_free(new_song);
437 return mpd_connection_clear_error(connection) ||
438 mpdclient_handle_error(c);
439 }
441 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
442 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
443 /* the cheap route: match on the new playlist length
444 and its version, we can keep our local playlist
445 copy in sync */
446 c->playlist.version = mpd_status_get_queue_version(status);
448 /* the song we just received has the correct id;
449 append it to the local playlist */
450 playlist_append(&c->playlist, new_song);
451 }
453 mpd_song_free(new_song);
455 return true;
456 }
458 bool
459 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
460 {
461 struct mpd_connection *connection = mpdclient_get_connection(c);
463 if (connection == NULL || c->status == NULL)
464 return false;
466 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
467 return false;
469 const struct mpd_song *song = playlist_get(&c->playlist, idx);
471 /* send the delete command to mpd; at the same time, get the
472 new status (to verify the playlist id) */
474 if (!mpd_command_list_begin(connection, false) ||
475 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
476 !mpd_send_status(connection) ||
477 !mpd_command_list_end(connection))
478 return mpdclient_handle_error(c);
480 c->events |= MPD_IDLE_QUEUE;
482 struct mpd_status *status = mpdclient_recv_status(c);
483 if (status == NULL)
484 return false;
486 if (!mpd_response_finish(connection))
487 return mpdclient_handle_error(c);
489 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
490 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
491 /* the cheap route: match on the new playlist length
492 and its version, we can keep our local playlist
493 copy in sync */
494 c->playlist.version = mpd_status_get_queue_version(status);
496 /* remove the song from the local playlist */
497 playlist_remove(&c->playlist, idx);
499 /* remove references to the song */
500 if (c->song == song)
501 c->song = NULL;
502 }
504 return true;
505 }
507 bool
508 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
509 {
510 if (end == start + 1)
511 /* if that's not really a range, we choose to use the
512 safer "deleteid" version */
513 return mpdclient_cmd_delete(c, start);
515 struct mpd_connection *connection = mpdclient_get_connection(c);
516 if (connection == NULL)
517 return false;
519 /* send the delete command to mpd; at the same time, get the
520 new status (to verify the playlist id) */
522 if (!mpd_command_list_begin(connection, false) ||
523 !mpd_send_delete_range(connection, start, end) ||
524 !mpd_send_status(connection) ||
525 !mpd_command_list_end(connection))
526 return mpdclient_handle_error(c);
528 c->events |= MPD_IDLE_QUEUE;
530 struct mpd_status *status = mpdclient_recv_status(c);
531 if (status == NULL)
532 return false;
534 if (!mpd_response_finish(connection))
535 return mpdclient_handle_error(c);
537 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
538 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
539 /* the cheap route: match on the new playlist length
540 and its version, we can keep our local playlist
541 copy in sync */
542 c->playlist.version = mpd_status_get_queue_version(status);
544 /* remove the song from the local playlist */
545 while (end > start) {
546 --end;
548 /* remove references to the song */
549 if (c->song == playlist_get(&c->playlist, end))
550 c->song = NULL;
552 playlist_remove(&c->playlist, end);
553 }
554 }
556 return true;
557 }
559 bool
560 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
561 {
562 if (dest_pos == src_pos)
563 return true;
565 struct mpd_connection *connection = mpdclient_get_connection(c);
566 if (connection == NULL)
567 return false;
569 /* send the "move" command to MPD; at the same time, get the
570 new status (to verify the playlist id) */
572 if (!mpd_command_list_begin(connection, false) ||
573 !mpd_send_move(connection, src_pos, dest_pos) ||
574 !mpd_send_status(connection) ||
575 !mpd_command_list_end(connection))
576 return mpdclient_handle_error(c);
578 c->events |= MPD_IDLE_QUEUE;
580 struct mpd_status *status = mpdclient_recv_status(c);
581 if (status == NULL)
582 return false;
584 if (!mpd_response_finish(connection))
585 return mpdclient_handle_error(c);
587 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
588 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
589 /* the cheap route: match on the new playlist length
590 and its version, we can keep our local playlist
591 copy in sync */
592 c->playlist.version = mpd_status_get_queue_version(status);
594 /* swap songs in the local playlist */
595 playlist_move(&c->playlist, dest_pos, src_pos);
596 }
598 return true;
599 }
601 #if LIBMPDCLIENT_CHECK_VERSION(2,5,0)
602 /* The client-to-client protocol (MPD 0.17.0) */
604 bool
605 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
606 {
607 struct mpd_connection *connection = mpdclient_get_connection(c);
609 if (connection == NULL)
610 return false;
612 if (!mpd_send_subscribe(connection, channel))
613 return mpdclient_handle_error(c);
615 return mpdclient_finish_command(c);
616 }
618 bool
619 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
620 {
621 struct mpd_connection *connection = mpdclient_get_connection(c);
622 if (connection == NULL)
623 return false;
625 if (!mpd_send_unsubscribe(connection, channel))
626 return mpdclient_handle_error(c);
628 return mpdclient_finish_command(c);
629 }
631 bool
632 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
633 const char *text)
634 {
635 struct mpd_connection *connection = mpdclient_get_connection(c);
636 if (connection == NULL)
637 return false;
639 if (!mpd_send_send_message(connection, channel, text))
640 return mpdclient_handle_error(c);
642 return mpdclient_finish_command(c);
643 }
645 bool
646 mpdclient_send_read_messages(struct mpdclient *c)
647 {
648 struct mpd_connection *connection = mpdclient_get_connection(c);
649 if (connection == NULL)
650 return false;
652 return mpd_send_read_messages(connection)?
653 true : mpdclient_handle_error(c);
654 }
656 struct mpd_message *
657 mpdclient_recv_message(struct mpdclient *c)
658 {
659 struct mpd_connection *connection = mpdclient_get_connection(c);
660 if (connection == NULL)
661 return false;
663 struct mpd_message *message = mpd_recv_message(connection);
664 if (message == NULL &&
665 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
666 mpdclient_handle_error(c);
668 return message;
669 }
670 #endif
672 /****************************************************************************/
673 /*** Playlist management functions ******************************************/
674 /****************************************************************************/
676 /* update playlist */
677 bool
678 mpdclient_playlist_update(struct mpdclient *c)
679 {
680 struct mpd_connection *connection = mpdclient_get_connection(c);
681 if (connection == NULL)
682 return false;
684 playlist_clear(&c->playlist);
686 mpd_send_list_queue_meta(connection);
688 struct mpd_entity *entity;
689 while ((entity = mpd_recv_entity(connection))) {
690 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
691 playlist_append(&c->playlist, mpd_entity_get_song(entity));
693 mpd_entity_free(entity);
694 }
696 c->playlist.version = mpd_status_get_queue_version(c->status);
697 c->song = NULL;
699 return mpdclient_finish_command(c);
700 }
702 /* update playlist (plchanges) */
703 bool
704 mpdclient_playlist_update_changes(struct mpdclient *c)
705 {
706 struct mpd_connection *connection = mpdclient_get_connection(c);
708 if (connection == NULL)
709 return false;
711 mpd_send_queue_changes_meta(connection, c->playlist.version);
713 struct mpd_song *song;
714 while ((song = mpd_recv_song(connection)) != NULL) {
715 int pos = mpd_song_get_pos(song);
717 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
718 /* update song */
719 playlist_replace(&c->playlist, pos, song);
720 } else {
721 /* add a new song */
722 playlist_append(&c->playlist, song);
723 }
725 mpd_song_free(song);
726 }
728 /* remove trailing songs */
730 unsigned length = mpd_status_get_queue_length(c->status);
731 while (length < c->playlist.list->len) {
732 guint pos = c->playlist.list->len - 1;
734 /* Remove the last playlist entry */
735 playlist_remove(&c->playlist, pos);
736 }
738 c->song = NULL;
739 c->playlist.version = mpd_status_get_queue_version(c->status);
741 return mpdclient_finish_command(c);
742 }
745 /****************************************************************************/
746 /*** Filelist functions *****************************************************/
747 /****************************************************************************/
749 bool
750 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
751 {
752 struct mpd_connection *connection = mpdclient_get_connection(c);
753 if (connection == NULL)
754 return false;
756 if (filelist_is_empty(fl))
757 return true;
759 mpd_command_list_begin(connection, false);
761 for (unsigned i = 0; i < filelist_length(fl); ++i) {
762 struct filelist_entry *entry = filelist_get(fl, i);
763 struct mpd_entity *entity = entry->entity;
765 if (entity != NULL &&
766 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
767 const struct mpd_song *song =
768 mpd_entity_get_song(entity);
770 mpd_send_add(connection, mpd_song_get_uri(song));
771 }
772 }
774 mpd_command_list_end(connection);
775 return mpdclient_finish_command(c);
776 }