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);
181 c->volume = -1;
183 if (connection == NULL)
184 return false;
186 /* always announce these options as long as we don't have
187 "idle" support */
188 if (c->source == NULL)
189 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
191 /* free the old status */
192 if (c->status)
193 mpd_status_free(c->status);
195 /* retrieve new status */
196 c->status = mpd_run_status(connection);
197 if (c->status == NULL)
198 return mpdclient_handle_error(c);
200 if (c->source == NULL &&
201 c->update_id != mpd_status_get_update_id(c->status)) {
202 c->events |= MPD_IDLE_UPDATE;
204 if (c->update_id > 0)
205 c->events |= MPD_IDLE_DATABASE;
206 }
208 c->update_id = mpd_status_get_update_id(c->status);
210 if (c->source == NULL &&
211 c->volume != mpd_status_get_volume(c->status))
212 c->events |= MPD_IDLE_MIXER;
214 c->volume = mpd_status_get_volume(c->status);
216 /* check if the playlist needs an update */
217 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
218 bool retval;
220 if (c->source == NULL)
221 c->events |= MPD_IDLE_QUEUE;
223 if (!playlist_is_empty(&c->playlist))
224 retval = mpdclient_playlist_update_changes(c);
225 else
226 retval = mpdclient_playlist_update(c);
227 if (!retval)
228 return false;
229 }
231 /* update the current song */
232 if (!c->song || mpd_status_get_song_id(c->status)) {
233 c->song = playlist_get_song(&c->playlist,
234 mpd_status_get_song_pos(c->status));
235 }
237 return true;
238 }
240 struct mpd_connection *
241 mpdclient_get_connection(struct mpdclient *c)
242 {
243 if (c->source != NULL && c->idle) {
244 c->idle = false;
245 mpd_glib_leave(c->source);
246 }
248 return c->connection;
249 }
251 void
252 mpdclient_put_connection(struct mpdclient *c)
253 {
254 assert(c->source == NULL || c->connection != NULL);
256 if (c->source != NULL && !c->idle) {
257 c->idle = mpd_glib_enter(c->source);
258 }
259 }
261 static struct mpd_status *
262 mpdclient_recv_status(struct mpdclient *c)
263 {
264 struct mpd_status *status;
266 assert(c->connection != NULL);
268 status = mpd_recv_status(c->connection);
269 if (status == NULL) {
270 mpdclient_handle_error(c);
271 return NULL;
272 }
274 if (c->status != NULL)
275 mpd_status_free(c->status);
276 return c->status = status;
277 }
279 /****************************************************************************/
280 /*** MPD Commands **********************************************************/
281 /****************************************************************************/
283 bool
284 mpdclient_cmd_crop(struct mpdclient *c)
285 {
286 struct mpd_connection *connection;
287 int length, current;
289 if (!mpdclient_is_playing(c))
290 return false;
292 length = mpd_status_get_queue_length(c->status);
293 current = mpd_status_get_song_pos(c->status);
294 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
295 return true;
297 connection = mpdclient_get_connection(c);
298 if (connection == NULL)
299 return false;
301 mpd_command_list_begin(connection, false);
303 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
304 if (current < length - 1)
305 mpd_send_delete_range(connection, current + 1, length);
306 if (current > 0)
307 mpd_send_delete_range(connection, 0, current);
308 } else
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 struct mpd_status *status;
324 if (connection == NULL)
325 return false;
327 /* send "clear" and "status" */
328 if (!mpd_command_list_begin(connection, false) ||
329 !mpd_send_clear(connection) ||
330 !mpd_send_status(connection) ||
331 !mpd_command_list_end(connection))
332 return mpdclient_handle_error(c);
334 /* receive the new status, store it in the mpdclient struct */
336 status = mpdclient_recv_status(c);
337 if (status == NULL)
338 return false;
340 if (!mpd_response_finish(connection))
341 return mpdclient_handle_error(c);
343 /* update mpdclient.playlist */
345 if (mpd_status_get_queue_length(status) == 0) {
346 /* after the "clear" command, the queue is really
347 empty - this means we can clear it locally,
348 reducing the UI latency */
349 playlist_clear(&c->playlist);
350 c->playlist.version = mpd_status_get_queue_version(status);
351 }
353 c->events |= MPD_IDLE_QUEUE;
354 return true;
355 }
357 bool
358 mpdclient_cmd_volume(struct mpdclient *c, gint value)
359 {
360 struct mpd_connection *connection = mpdclient_get_connection(c);
361 if (connection == NULL)
362 return false;
364 mpd_send_set_volume(connection, value);
365 return mpdclient_finish_command(c);
366 }
368 bool
369 mpdclient_cmd_volume_up(struct mpdclient *c)
370 {
371 struct mpd_connection *connection = mpdclient_get_connection(c);
372 if (connection == NULL)
373 return false;
375 if (c->status == NULL ||
376 mpd_status_get_volume(c->status) == -1)
377 return true;
379 if (c->volume < 0)
380 c->volume = mpd_status_get_volume(c->status);
382 if (c->volume >= 100)
383 return true;
385 return mpdclient_cmd_volume(c, ++c->volume);
386 }
388 bool
389 mpdclient_cmd_volume_down(struct mpdclient *c)
390 {
391 struct mpd_connection *connection = mpdclient_get_connection(c);
392 if (connection == NULL)
393 return false;
395 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
396 return true;
398 if (c->volume < 0)
399 c->volume = mpd_status_get_volume(c->status);
401 if (c->volume <= 0)
402 return true;
404 return mpdclient_cmd_volume(c, --c->volume);
405 }
407 bool
408 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
409 {
410 struct mpd_connection *connection = mpdclient_get_connection(c);
411 if (connection == NULL)
412 return false;
414 mpd_send_add(connection, path_utf8);
415 return mpdclient_finish_command(c);
416 }
418 bool
419 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
420 {
421 struct mpd_connection *connection = mpdclient_get_connection(c);
422 struct mpd_status *status;
423 struct mpd_song *new_song;
425 assert(c != NULL);
426 assert(song != NULL);
428 if (connection == NULL || c->status == NULL)
429 return false;
431 /* send the add command to mpd; at the same time, get the new
432 status (to verify the new playlist id) and the last song
433 (we hope that's the song we just added) */
435 if (!mpd_command_list_begin(connection, true) ||
436 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
437 !mpd_send_status(connection) ||
438 !mpd_send_get_queue_song_pos(connection,
439 playlist_length(&c->playlist)) ||
440 !mpd_command_list_end(connection) ||
441 !mpd_response_next(connection))
442 return mpdclient_handle_error(c);
444 c->events |= MPD_IDLE_QUEUE;
446 status = mpdclient_recv_status(c);
447 if (status == NULL)
448 return false;
450 if (!mpd_response_next(connection))
451 return mpdclient_handle_error(c);
453 new_song = mpd_recv_song(connection);
454 if (!mpd_response_finish(connection) || new_song == NULL) {
455 if (new_song != NULL)
456 mpd_song_free(new_song);
458 return mpd_connection_clear_error(connection) ||
459 mpdclient_handle_error(c);
460 }
462 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
463 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
464 /* the cheap route: match on the new playlist length
465 and its version, we can keep our local playlist
466 copy in sync */
467 c->playlist.version = mpd_status_get_queue_version(status);
469 /* the song we just received has the correct id;
470 append it to the local playlist */
471 playlist_append(&c->playlist, new_song);
472 }
474 mpd_song_free(new_song);
476 return true;
477 }
479 bool
480 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
481 {
482 struct mpd_connection *connection = mpdclient_get_connection(c);
483 const struct mpd_song *song;
484 struct mpd_status *status;
486 if (connection == NULL || c->status == NULL)
487 return false;
489 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
490 return false;
492 song = playlist_get(&c->playlist, idx);
494 /* send the delete command to mpd; at the same time, get the
495 new status (to verify the playlist id) */
497 if (!mpd_command_list_begin(connection, false) ||
498 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
499 !mpd_send_status(connection) ||
500 !mpd_command_list_end(connection))
501 return mpdclient_handle_error(c);
503 c->events |= MPD_IDLE_QUEUE;
505 status = mpdclient_recv_status(c);
506 if (status == NULL)
507 return false;
509 if (!mpd_response_finish(connection))
510 return mpdclient_handle_error(c);
512 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
513 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
514 /* the cheap route: match on the new playlist length
515 and its version, we can keep our local playlist
516 copy in sync */
517 c->playlist.version = mpd_status_get_queue_version(status);
519 /* remove the song from the local playlist */
520 playlist_remove(&c->playlist, idx);
522 /* remove references to the song */
523 if (c->song == song)
524 c->song = NULL;
525 }
527 return true;
528 }
530 /**
531 * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
532 * It emulates the "delete range" command with a list of simple
533 * "delete" commands.
534 */
535 static bool
536 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
537 unsigned start, unsigned end)
538 {
539 struct mpd_connection *connection = mpdclient_get_connection(c);
540 if (connection == NULL)
541 return false;
543 if (!mpd_command_list_begin(connection, false))
544 return mpdclient_handle_error(c);
546 for (; start < end; --end)
547 mpd_send_delete(connection, start);
549 if (!mpd_command_list_end(connection) ||
550 !mpd_response_finish(connection))
551 return mpdclient_handle_error(c);
553 return true;
554 }
556 bool
557 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
558 {
559 struct mpd_connection *connection;
560 struct mpd_status *status;
562 if (end == start + 1)
563 /* if that's not really a range, we choose to use the
564 safer "deleteid" version */
565 return mpdclient_cmd_delete(c, start);
567 connection = mpdclient_get_connection(c);
568 if (connection == NULL)
569 return false;
571 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
572 return mpdclient_cmd_delete_range_fallback(c, start, end);
574 /* MPD 0.16 supports "delete" with a range argument */
576 /* send the delete command to mpd; at the same time, get the
577 new status (to verify the playlist id) */
579 if (!mpd_command_list_begin(connection, false) ||
580 !mpd_send_delete_range(connection, start, end) ||
581 !mpd_send_status(connection) ||
582 !mpd_command_list_end(connection))
583 return mpdclient_handle_error(c);
585 c->events |= MPD_IDLE_QUEUE;
587 status = mpdclient_recv_status(c);
588 if (status == NULL)
589 return false;
591 if (!mpd_response_finish(connection))
592 return mpdclient_handle_error(c);
594 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
595 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
596 /* the cheap route: match on the new playlist length
597 and its version, we can keep our local playlist
598 copy in sync */
599 c->playlist.version = mpd_status_get_queue_version(status);
601 /* remove the song from the local playlist */
602 while (end > start) {
603 --end;
605 /* remove references to the song */
606 if (c->song == playlist_get(&c->playlist, end))
607 c->song = NULL;
609 playlist_remove(&c->playlist, end);
610 }
611 }
613 return true;
614 }
616 bool
617 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
618 {
619 struct mpd_connection *connection;
620 struct mpd_status *status;
622 if (dest_pos == src_pos)
623 return true;
625 connection = mpdclient_get_connection(c);
626 if (connection == NULL)
627 return false;
629 /* send the "move" command to MPD; at the same time, get the
630 new status (to verify the playlist id) */
632 if (!mpd_command_list_begin(connection, false) ||
633 !mpd_send_move(connection, src_pos, dest_pos) ||
634 !mpd_send_status(connection) ||
635 !mpd_command_list_end(connection))
636 return mpdclient_handle_error(c);
638 c->events |= MPD_IDLE_QUEUE;
640 status = mpdclient_recv_status(c);
641 if (status == NULL)
642 return false;
644 if (!mpd_response_finish(connection))
645 return mpdclient_handle_error(c);
647 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
648 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
649 /* the cheap route: match on the new playlist length
650 and its version, we can keep our local playlist
651 copy in sync */
652 c->playlist.version = mpd_status_get_queue_version(status);
654 /* swap songs in the local playlist */
655 playlist_move(&c->playlist, dest_pos, src_pos);
656 }
658 return true;
659 }
662 /****************************************************************************/
663 /*** Playlist management functions ******************************************/
664 /****************************************************************************/
666 /* update playlist */
667 bool
668 mpdclient_playlist_update(struct mpdclient *c)
669 {
670 struct mpd_connection *connection = mpdclient_get_connection(c);
671 struct mpd_entity *entity;
673 if (connection == NULL)
674 return false;
676 playlist_clear(&c->playlist);
678 mpd_send_list_queue_meta(connection);
679 while ((entity = mpd_recv_entity(connection))) {
680 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
681 playlist_append(&c->playlist, mpd_entity_get_song(entity));
683 mpd_entity_free(entity);
684 }
686 c->playlist.version = mpd_status_get_queue_version(c->status);
687 c->song = NULL;
689 return mpdclient_finish_command(c);
690 }
692 /* update playlist (plchanges) */
693 bool
694 mpdclient_playlist_update_changes(struct mpdclient *c)
695 {
696 struct mpd_connection *connection = mpdclient_get_connection(c);
697 struct mpd_song *song;
698 guint length;
700 if (connection == NULL)
701 return false;
703 mpd_send_queue_changes_meta(connection, c->playlist.version);
705 while ((song = mpd_recv_song(connection)) != NULL) {
706 int pos = mpd_song_get_pos(song);
708 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
709 /* update song */
710 playlist_replace(&c->playlist, pos, song);
711 } else {
712 /* add a new song */
713 playlist_append(&c->playlist, song);
714 }
716 mpd_song_free(song);
717 }
719 /* remove trailing songs */
721 length = mpd_status_get_queue_length(c->status);
722 while (length < c->playlist.list->len) {
723 guint pos = c->playlist.list->len - 1;
725 /* Remove the last playlist entry */
726 playlist_remove(&c->playlist, pos);
727 }
729 c->song = NULL;
730 c->playlist.version = mpd_status_get_queue_version(c->status);
732 return mpdclient_finish_command(c);
733 }
736 /****************************************************************************/
737 /*** Filelist functions *****************************************************/
738 /****************************************************************************/
740 bool
741 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
742 {
743 struct mpd_connection *connection = mpdclient_get_connection(c);
744 guint i;
746 if (connection == NULL)
747 return false;
749 if (filelist_is_empty(fl))
750 return true;
752 mpd_command_list_begin(connection, false);
754 for (i = 0; i < filelist_length(fl); ++i) {
755 struct filelist_entry *entry = filelist_get(fl, i);
756 struct mpd_entity *entity = entry->entity;
758 if (entity != NULL &&
759 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
760 const struct mpd_song *song =
761 mpd_entity_get_song(entity);
763 mpd_send_add(connection, mpd_song_get_uri(song));
764 }
765 }
767 mpd_command_list_end(connection);
768 return mpdclient_finish_command(c);
769 }