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 = mpd_glib_enter(c->source);
256 }
257 }
260 /****************************************************************************/
261 /*** MPD Commands **********************************************************/
262 /****************************************************************************/
264 bool
265 mpdclient_cmd_crop(struct mpdclient *c)
266 {
267 struct mpd_connection *connection;
268 int length, current;
270 if (c->status == NULL)
271 return false;
273 length = mpd_status_get_queue_length(c->status);
274 current = mpd_status_get_song_pos(c->status);
275 if (current < 0 ||
276 (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
277 mpd_status_get_state(c->status) != MPD_STATE_PAUSE) ||
278 mpd_status_get_queue_length(c->status) < 2)
279 return true;
281 connection = mpdclient_get_connection(c);
282 if (connection == NULL)
283 return false;
285 mpd_command_list_begin(connection, false);
287 while (--length >= 0)
288 if (length != current)
289 mpd_send_delete(connection, length);
291 mpd_command_list_end(connection);
293 return mpdclient_finish_command(c);
294 }
296 bool
297 mpdclient_cmd_clear(struct mpdclient *c)
298 {
299 struct mpd_connection *connection = mpdclient_get_connection(c);
300 struct mpd_status *status;
302 if (connection == NULL)
303 return false;
305 /* send "clear" and "status" */
306 if (!mpd_command_list_begin(connection, false) ||
307 !mpd_send_clear(connection) ||
308 !mpd_send_status(connection) ||
309 !mpd_command_list_end(connection))
310 return mpdclient_handle_error(c);
312 /* receive the new status, store it in the mpdclient struct */
314 status = mpd_recv_status(connection);
315 if (status == NULL)
316 return mpdclient_handle_error(c);
318 if (c->status != NULL)
319 mpd_status_free(c->status);
320 c->status = status;
322 if (!mpd_response_finish(connection))
323 return mpdclient_handle_error(c);
325 /* update mpdclient.playlist */
327 if (mpd_status_get_queue_length(status) == 0) {
328 /* after the "clear" command, the queue is really
329 empty - this means we can clear it locally,
330 reducing the UI latency */
331 playlist_clear(&c->playlist);
332 c->playlist.version = mpd_status_get_queue_version(status);
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 mpd_send_add(connection, path_utf8);
397 return mpdclient_finish_command(c);
398 }
400 bool
401 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
402 {
403 struct mpd_connection *connection = mpdclient_get_connection(c);
404 struct mpd_status *status;
405 struct mpd_song *new_song;
407 assert(c != NULL);
408 assert(song != NULL);
410 if (connection == NULL || c->status == NULL)
411 return false;
413 /* send the add command to mpd; at the same time, get the new
414 status (to verify the new playlist id) and the last song
415 (we hope that's the song we just added) */
417 if (!mpd_command_list_begin(connection, true) ||
418 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
419 !mpd_send_status(connection) ||
420 !mpd_send_get_queue_song_pos(connection,
421 playlist_length(&c->playlist)) ||
422 !mpd_command_list_end(connection) ||
423 !mpd_response_next(connection))
424 return mpdclient_handle_error(c);
426 c->events |= MPD_IDLE_PLAYLIST;
428 status = mpd_recv_status(connection);
429 if (status != NULL) {
430 if (c->status != NULL)
431 mpd_status_free(c->status);
432 c->status = status;
433 }
435 if (!mpd_response_next(connection))
436 return mpdclient_handle_error(c);
438 new_song = mpd_recv_song(connection);
439 if (!mpd_response_finish(connection) || new_song == NULL) {
440 if (new_song != NULL)
441 mpd_song_free(new_song);
443 return mpd_connection_clear_error(connection) ||
444 mpdclient_handle_error(c);
445 }
447 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
448 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
449 /* the cheap route: match on the new playlist length
450 and its version, we can keep our local playlist
451 copy in sync */
452 c->playlist.version = mpd_status_get_queue_version(status);
454 /* the song we just received has the correct id;
455 append it to the local playlist */
456 playlist_append(&c->playlist, new_song);
457 }
459 mpd_song_free(new_song);
461 return true;
462 }
464 bool
465 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
466 {
467 struct mpd_connection *connection = mpdclient_get_connection(c);
468 const struct mpd_song *song;
469 struct mpd_status *status;
471 if (connection == NULL || c->status == NULL)
472 return false;
474 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
475 return false;
477 song = playlist_get(&c->playlist, idx);
479 /* send the delete command to mpd; at the same time, get the
480 new status (to verify the playlist id) */
482 if (!mpd_command_list_begin(connection, false) ||
483 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
484 !mpd_send_status(connection) ||
485 !mpd_command_list_end(connection))
486 return mpdclient_handle_error(c);
488 c->events |= MPD_IDLE_PLAYLIST;
490 status = mpd_recv_status(connection);
491 if (status != NULL) {
492 if (c->status != NULL)
493 mpd_status_free(c->status);
494 c->status = status;
495 }
497 if (!mpd_response_finish(connection))
498 return mpdclient_handle_error(c);
500 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
501 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
502 /* the cheap route: match on the new playlist length
503 and its version, we can keep our local playlist
504 copy in sync */
505 c->playlist.version = mpd_status_get_queue_version(status);
507 /* remove the song from the local playlist */
508 playlist_remove(&c->playlist, idx);
510 /* remove references to the song */
511 if (c->song == song)
512 c->song = NULL;
513 }
515 return true;
516 }
518 /**
519 * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
520 * It emulates the "delete range" command with a list of simple
521 * "delete" commands.
522 */
523 static bool
524 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
525 unsigned start, unsigned end)
526 {
527 struct mpd_connection *connection = mpdclient_get_connection(c);
528 if (connection == NULL)
529 return false;
531 if (!mpd_command_list_begin(connection, false))
532 return mpdclient_handle_error(c);
534 for (; start < end; --end)
535 mpd_send_delete(connection, start);
537 if (!mpd_command_list_end(connection) ||
538 !mpd_response_finish(connection))
539 return mpdclient_handle_error(c);
541 return true;
542 }
544 bool
545 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
546 {
547 struct mpd_connection *connection;
548 struct mpd_status *status;
550 if (end == start + 1)
551 /* if that's not really a range, we choose to use the
552 safer "deleteid" version */
553 return mpdclient_cmd_delete(c, start);
555 connection = mpdclient_get_connection(c);
556 if (connection == NULL)
557 return false;
559 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
560 return mpdclient_cmd_delete_range_fallback(c, start, end);
562 /* MPD 0.16 supports "delete" with a range argument */
564 /* send the delete command to mpd; at the same time, get the
565 new status (to verify the playlist id) */
567 if (!mpd_command_list_begin(connection, false) ||
568 !mpd_send_delete_range(connection, start, end) ||
569 !mpd_send_status(connection) ||
570 !mpd_command_list_end(connection))
571 return mpdclient_handle_error(c);
573 c->events |= MPD_IDLE_PLAYLIST;
575 status = mpd_recv_status(connection);
576 if (status != NULL) {
577 if (c->status != NULL)
578 mpd_status_free(c->status);
579 c->status = status;
580 }
582 if (!mpd_response_finish(connection))
583 return mpdclient_handle_error(c);
585 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
586 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
587 /* the cheap route: match on the new playlist length
588 and its version, we can keep our local playlist
589 copy in sync */
590 c->playlist.version = mpd_status_get_queue_version(status);
592 /* remove the song from the local playlist */
593 while (end > start) {
594 --end;
596 /* remove references to the song */
597 if (c->song == playlist_get(&c->playlist, end))
598 c->song = NULL;
600 playlist_remove(&c->playlist, end);
601 }
602 }
604 return true;
605 }
607 bool
608 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
609 {
610 struct mpd_connection *connection = mpdclient_get_connection(c);
611 const struct mpd_song *song1, *song2;
612 struct mpd_status *status;
614 if (connection == NULL)
615 return false;
617 if (old_index == new_index || new_index < 0 ||
618 (guint)new_index >= c->playlist.list->len)
619 return false;
621 song1 = playlist_get(&c->playlist, old_index);
622 song2 = playlist_get(&c->playlist, new_index);
624 /* send the delete command to mpd; at the same time, get the
625 new status (to verify the playlist id) */
627 if (!mpd_command_list_begin(connection, false) ||
628 !mpd_send_swap_id(connection, mpd_song_get_id(song1),
629 mpd_song_get_id(song2)) ||
630 !mpd_send_status(connection) ||
631 !mpd_command_list_end(connection))
632 return mpdclient_handle_error(c);
634 c->events |= MPD_IDLE_PLAYLIST;
636 status = mpd_recv_status(connection);
637 if (status != NULL) {
638 if (c->status != NULL)
639 mpd_status_free(c->status);
640 c->status = status;
641 }
643 if (!mpd_response_finish(connection))
644 return mpdclient_handle_error(c);
646 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
647 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
648 /* the cheap route: match on the new playlist length
649 and its version, we can keep our local playlist
650 copy in sync */
651 c->playlist.version = mpd_status_get_queue_version(status);
653 /* swap songs in the local playlist */
654 playlist_swap(&c->playlist, old_index, new_index);
655 }
657 return true;
658 }
661 /****************************************************************************/
662 /*** Playlist management functions ******************************************/
663 /****************************************************************************/
665 /* update playlist */
666 bool
667 mpdclient_playlist_update(struct mpdclient *c)
668 {
669 struct mpd_connection *connection = mpdclient_get_connection(c);
670 struct mpd_entity *entity;
672 if (connection == NULL)
673 return false;
675 playlist_clear(&c->playlist);
677 mpd_send_list_queue_meta(connection);
678 while ((entity = mpd_recv_entity(connection))) {
679 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
680 playlist_append(&c->playlist, mpd_entity_get_song(entity));
682 mpd_entity_free(entity);
683 }
685 c->playlist.version = mpd_status_get_queue_version(c->status);
686 c->song = NULL;
688 return mpdclient_finish_command(c);
689 }
691 /* update playlist (plchanges) */
692 bool
693 mpdclient_playlist_update_changes(struct mpdclient *c)
694 {
695 struct mpd_connection *connection = mpdclient_get_connection(c);
696 struct mpd_song *song;
697 guint length;
699 if (connection == NULL)
700 return false;
702 mpd_send_queue_changes_meta(connection, c->playlist.version);
704 while ((song = mpd_recv_song(connection)) != NULL) {
705 int pos = mpd_song_get_pos(song);
707 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
708 /* update song */
709 playlist_replace(&c->playlist, pos, song);
710 } else {
711 /* add a new song */
712 playlist_append(&c->playlist, song);
713 }
715 mpd_song_free(song);
716 }
718 /* remove trailing songs */
720 length = mpd_status_get_queue_length(c->status);
721 while (length < c->playlist.list->len) {
722 guint pos = c->playlist.list->len - 1;
724 /* Remove the last playlist entry */
725 playlist_remove(&c->playlist, pos);
726 }
728 c->song = NULL;
729 c->playlist.version = mpd_status_get_queue_version(c->status);
731 return mpdclient_finish_command(c);
732 }
735 /****************************************************************************/
736 /*** Filelist functions *****************************************************/
737 /****************************************************************************/
739 bool
740 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
741 {
742 struct mpd_connection *connection = mpdclient_get_connection(c);
743 guint i;
745 if (connection == NULL)
746 return false;
748 if (filelist_is_empty(fl))
749 return true;
751 mpd_command_list_begin(connection, false);
753 for (i = 0; i < filelist_length(fl); ++i) {
754 struct filelist_entry *entry = filelist_get(fl, i);
755 struct mpd_entity *entity = entry->entity;
757 if (entity != NULL &&
758 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
759 const struct mpd_song *song =
760 mpd_entity_get_song(entity);
762 mpd_send_add(connection, mpd_song_get_uri(song));
763 }
764 }
766 mpd_command_list_end(connection);
767 return mpdclient_finish_command(c);
768 }