1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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 "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "gidle.h"
25 #include "charset.h"
27 #include <mpd/client.h>
29 #include <assert.h>
31 static void
32 mpdclient_invoke_error_callback(enum mpd_error error,
33 const char *message)
34 {
35 char *allocated;
36 if (error == MPD_ERROR_SERVER)
37 /* server errors are UTF-8, the others are locale */
38 message = allocated = utf8_to_locale(message);
39 else
40 allocated = NULL;
42 mpdclient_error_callback(message);
43 g_free(allocated);
44 }
46 static void
47 mpdclient_gidle_callback(enum mpd_error error,
48 gcc_unused enum mpd_server_error server_error,
49 const char *message, enum mpd_idle events,
50 void *ctx)
51 {
52 struct mpdclient *c = ctx;
54 c->idle = false;
56 assert(mpdclient_is_connected(c));
58 if (error != MPD_ERROR_SUCCESS) {
59 mpdclient_invoke_error_callback(error, message);
60 mpdclient_disconnect(c);
61 mpdclient_lost_callback();
62 return;
63 }
65 c->events |= events;
66 mpdclient_update(c);
68 mpdclient_idle_callback(c->events);
70 c->events = 0;
72 mpdclient_put_connection(c);
73 }
75 /****************************************************************************/
76 /*** mpdclient functions ****************************************************/
77 /****************************************************************************/
79 bool
80 mpdclient_handle_error(struct mpdclient *c)
81 {
82 enum mpd_error error = mpd_connection_get_error(c->connection);
84 assert(error != MPD_ERROR_SUCCESS);
86 if (error == MPD_ERROR_SERVER &&
87 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
88 mpdclient_auth_callback(c))
89 return true;
91 mpdclient_invoke_error_callback(error,
92 mpd_connection_get_error_message(c->connection));
94 if (!mpd_connection_clear_error(c->connection))
95 mpdclient_disconnect(c);
97 return false;
98 }
100 struct mpdclient *
101 mpdclient_new(const gchar *host, unsigned port,
102 unsigned timeout_ms, const gchar *password)
103 {
104 struct mpdclient *c = g_new0(struct mpdclient, 1);
106 c->host = host;
107 c->port = port;
108 c->timeout_ms = timeout_ms;
109 c->password = password;
111 playlist_init(&c->playlist);
112 c->volume = -1;
113 c->events = 0;
114 c->playing = false;
116 return c;
117 }
119 void
120 mpdclient_free(struct mpdclient *c)
121 {
122 mpdclient_disconnect(c);
124 mpdclient_playlist_free(&c->playlist);
126 g_free(c);
127 }
129 static void
130 mpdclient_status_free(struct mpdclient *c)
131 {
132 if (c->status == NULL)
133 return;
135 mpd_status_free(c->status);
136 c->status = NULL;
138 c->volume = -1;
139 c->playing = false;
140 }
142 void
143 mpdclient_disconnect(struct mpdclient *c)
144 {
145 if (c->source != NULL) {
146 mpd_glib_free(c->source);
147 c->source = NULL;
148 c->idle = false;
149 }
151 if (c->connection) {
152 mpd_connection_free(c->connection);
153 ++c->connection_id;
154 }
155 c->connection = NULL;
157 mpdclient_status_free(c);
159 playlist_clear(&c->playlist);
161 if (c->song)
162 c->song = NULL;
164 /* everything has changed after a disconnect */
165 c->events |= MPD_IDLE_ALL;
166 }
168 bool
169 mpdclient_connect(struct mpdclient *c)
170 {
171 /* close any open connection */
172 mpdclient_disconnect(c);
174 /* connect to MPD */
175 c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms);
176 if (c->connection == NULL)
177 g_error("Out of memory");
179 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
180 mpdclient_handle_error(c);
181 mpdclient_disconnect(c);
182 mpdclient_failed_callback();
183 return false;
184 }
186 /* send password */
187 if (c->password != NULL &&
188 !mpd_run_password(c->connection, c->password)) {
189 mpdclient_handle_error(c);
190 mpdclient_disconnect(c);
191 mpdclient_failed_callback();
192 return false;
193 }
195 c->source = mpd_glib_new(c->connection,
196 mpdclient_gidle_callback, c);
198 ++c->connection_id;
200 mpdclient_connected_callback();
202 return true;
203 }
205 bool
206 mpdclient_update(struct mpdclient *c)
207 {
208 struct mpd_connection *connection = mpdclient_get_connection(c);
210 if (connection == NULL)
211 return false;
213 /* free the old status */
214 mpdclient_status_free(c);
216 /* retrieve new status */
217 c->status = mpd_run_status(connection);
218 if (c->status == NULL)
219 return mpdclient_handle_error(c);
221 c->volume = mpd_status_get_volume(c->status);
222 c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
224 /* check if the playlist needs an update */
225 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
226 bool retval;
228 if (!playlist_is_empty(&c->playlist))
229 retval = mpdclient_playlist_update_changes(c);
230 else
231 retval = mpdclient_playlist_update(c);
232 if (!retval)
233 return false;
234 }
236 /* update the current song */
237 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
238 c->song = playlist_get_song(&c->playlist,
239 mpd_status_get_song_pos(c->status));
240 }
242 return true;
243 }
245 struct mpd_connection *
246 mpdclient_get_connection(struct mpdclient *c)
247 {
248 if (c->source != NULL && c->idle) {
249 c->idle = false;
250 mpd_glib_leave(c->source);
251 }
253 return c->connection;
254 }
256 void
257 mpdclient_put_connection(struct mpdclient *c)
258 {
259 assert(c->source == NULL || c->connection != NULL);
261 if (c->source != NULL && !c->idle) {
262 c->idle = mpd_glib_enter(c->source);
263 }
264 }
266 static struct mpd_status *
267 mpdclient_recv_status(struct mpdclient *c)
268 {
269 assert(c->connection != NULL);
271 struct mpd_status *status = mpd_recv_status(c->connection);
272 if (status == NULL) {
273 mpdclient_handle_error(c);
274 return NULL;
275 }
277 if (c->status != NULL)
278 mpd_status_free(c->status);
279 return c->status = status;
280 }
282 /****************************************************************************/
283 /*** MPD Commands **********************************************************/
284 /****************************************************************************/
286 bool
287 mpdclient_cmd_crop(struct mpdclient *c)
288 {
289 if (!mpdclient_is_playing(c))
290 return false;
292 int length = mpd_status_get_queue_length(c->status);
293 int current = mpd_status_get_song_pos(c->status);
294 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
295 return true;
297 struct mpd_connection *connection = mpdclient_get_connection(c);
298 if (connection == NULL)
299 return false;
301 mpd_command_list_begin(connection, false);
303 if (current < length - 1)
304 mpd_send_delete_range(connection, current + 1, length);
305 if (current > 0)
306 mpd_send_delete_range(connection, 0, current);
308 mpd_command_list_end(connection);
310 return mpdclient_finish_command(c);
311 }
313 bool
314 mpdclient_cmd_clear(struct mpdclient *c)
315 {
316 struct mpd_connection *connection = mpdclient_get_connection(c);
317 if (connection == NULL)
318 return false;
320 /* send "clear" and "status" */
321 if (!mpd_command_list_begin(connection, false) ||
322 !mpd_send_clear(connection) ||
323 !mpd_send_status(connection) ||
324 !mpd_command_list_end(connection))
325 return mpdclient_handle_error(c);
327 /* receive the new status, store it in the mpdclient struct */
329 struct mpd_status *status = mpdclient_recv_status(c);
330 if (status == NULL)
331 return false;
333 if (!mpd_response_finish(connection))
334 return mpdclient_handle_error(c);
336 /* update mpdclient.playlist */
338 if (mpd_status_get_queue_length(status) == 0) {
339 /* after the "clear" command, the queue is really
340 empty - this means we can clear it locally,
341 reducing the UI latency */
342 playlist_clear(&c->playlist);
343 c->playlist.version = mpd_status_get_queue_version(status);
344 c->song = NULL;
345 }
347 c->events |= MPD_IDLE_QUEUE;
348 return true;
349 }
351 bool
352 mpdclient_cmd_volume(struct mpdclient *c, gint value)
353 {
354 struct mpd_connection *connection = mpdclient_get_connection(c);
355 if (connection == NULL)
356 return false;
358 mpd_send_set_volume(connection, value);
359 return mpdclient_finish_command(c);
360 }
362 bool
363 mpdclient_cmd_volume_up(struct mpdclient *c)
364 {
365 struct mpd_connection *connection = mpdclient_get_connection(c);
366 if (connection == NULL)
367 return false;
369 if (c->status == NULL ||
370 mpd_status_get_volume(c->status) == -1)
371 return true;
373 if (c->volume < 0)
374 c->volume = mpd_status_get_volume(c->status);
376 if (c->volume >= 100)
377 return true;
379 return mpdclient_cmd_volume(c, ++c->volume);
380 }
382 bool
383 mpdclient_cmd_volume_down(struct mpdclient *c)
384 {
385 struct mpd_connection *connection = mpdclient_get_connection(c);
386 if (connection == NULL)
387 return false;
389 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
390 return true;
392 if (c->volume < 0)
393 c->volume = mpd_status_get_volume(c->status);
395 if (c->volume <= 0)
396 return true;
398 return mpdclient_cmd_volume(c, --c->volume);
399 }
401 bool
402 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
403 {
404 struct mpd_connection *connection = mpdclient_get_connection(c);
405 if (connection == NULL)
406 return false;
408 return mpd_send_add(connection, path_utf8)?
409 mpdclient_finish_command(c) : false;
410 }
412 bool
413 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
414 {
415 assert(c != NULL);
416 assert(song != NULL);
418 struct mpd_connection *connection = mpdclient_get_connection(c);
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 struct mpd_status *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 struct mpd_song *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);
475 if (connection == NULL || c->status == NULL)
476 return false;
478 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
479 return false;
481 const struct mpd_song *song = playlist_get(&c->playlist, idx);
483 /* send the delete command to mpd; at the same time, get the
484 new status (to verify the playlist id) */
486 if (!mpd_command_list_begin(connection, false) ||
487 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
488 !mpd_send_status(connection) ||
489 !mpd_command_list_end(connection))
490 return mpdclient_handle_error(c);
492 c->events |= MPD_IDLE_QUEUE;
494 struct mpd_status *status = mpdclient_recv_status(c);
495 if (status == NULL)
496 return false;
498 if (!mpd_response_finish(connection))
499 return mpdclient_handle_error(c);
501 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
502 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
503 /* the cheap route: match on the new playlist length
504 and its version, we can keep our local playlist
505 copy in sync */
506 c->playlist.version = mpd_status_get_queue_version(status);
508 /* remove the song from the local playlist */
509 playlist_remove(&c->playlist, idx);
511 /* remove references to the song */
512 if (c->song == song)
513 c->song = NULL;
514 }
516 return true;
517 }
519 bool
520 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
521 {
522 if (end == start + 1)
523 /* if that's not really a range, we choose to use the
524 safer "deleteid" version */
525 return mpdclient_cmd_delete(c, start);
527 struct mpd_connection *connection = mpdclient_get_connection(c);
528 if (connection == NULL)
529 return false;
531 /* send the delete command to mpd; at the same time, get the
532 new status (to verify the playlist id) */
534 if (!mpd_command_list_begin(connection, false) ||
535 !mpd_send_delete_range(connection, start, end) ||
536 !mpd_send_status(connection) ||
537 !mpd_command_list_end(connection))
538 return mpdclient_handle_error(c);
540 c->events |= MPD_IDLE_QUEUE;
542 struct mpd_status *status = mpdclient_recv_status(c);
543 if (status == NULL)
544 return false;
546 if (!mpd_response_finish(connection))
547 return mpdclient_handle_error(c);
549 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
550 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
551 /* the cheap route: match on the new playlist length
552 and its version, we can keep our local playlist
553 copy in sync */
554 c->playlist.version = mpd_status_get_queue_version(status);
556 /* remove the song from the local playlist */
557 while (end > start) {
558 --end;
560 /* remove references to the song */
561 if (c->song == playlist_get(&c->playlist, end))
562 c->song = NULL;
564 playlist_remove(&c->playlist, end);
565 }
566 }
568 return true;
569 }
571 bool
572 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
573 {
574 if (dest_pos == src_pos)
575 return true;
577 struct mpd_connection *connection = mpdclient_get_connection(c);
578 if (connection == NULL)
579 return false;
581 /* send the "move" command to MPD; at the same time, get the
582 new status (to verify the playlist id) */
584 if (!mpd_command_list_begin(connection, false) ||
585 !mpd_send_move(connection, src_pos, dest_pos) ||
586 !mpd_send_status(connection) ||
587 !mpd_command_list_end(connection))
588 return mpdclient_handle_error(c);
590 c->events |= MPD_IDLE_QUEUE;
592 struct mpd_status *status = mpdclient_recv_status(c);
593 if (status == NULL)
594 return false;
596 if (!mpd_response_finish(connection))
597 return mpdclient_handle_error(c);
599 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
600 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
601 /* the cheap route: match on the new playlist length
602 and its version, we can keep our local playlist
603 copy in sync */
604 c->playlist.version = mpd_status_get_queue_version(status);
606 /* swap songs in the local playlist */
607 playlist_move(&c->playlist, dest_pos, src_pos);
608 }
610 return true;
611 }
613 /* The client-to-client protocol (MPD 0.17.0) */
615 bool
616 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
617 {
618 struct mpd_connection *connection = mpdclient_get_connection(c);
620 if (connection == NULL)
621 return false;
623 if (!mpd_send_subscribe(connection, channel))
624 return mpdclient_handle_error(c);
626 return mpdclient_finish_command(c);
627 }
629 bool
630 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
631 {
632 struct mpd_connection *connection = mpdclient_get_connection(c);
633 if (connection == NULL)
634 return false;
636 if (!mpd_send_unsubscribe(connection, channel))
637 return mpdclient_handle_error(c);
639 return mpdclient_finish_command(c);
640 }
642 bool
643 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
644 const char *text)
645 {
646 struct mpd_connection *connection = mpdclient_get_connection(c);
647 if (connection == NULL)
648 return false;
650 if (!mpd_send_send_message(connection, channel, text))
651 return mpdclient_handle_error(c);
653 return mpdclient_finish_command(c);
654 }
656 bool
657 mpdclient_send_read_messages(struct mpdclient *c)
658 {
659 struct mpd_connection *connection = mpdclient_get_connection(c);
660 if (connection == NULL)
661 return false;
663 return mpd_send_read_messages(connection)?
664 true : mpdclient_handle_error(c);
665 }
667 struct mpd_message *
668 mpdclient_recv_message(struct mpdclient *c)
669 {
670 struct mpd_connection *connection = mpdclient_get_connection(c);
671 if (connection == NULL)
672 return false;
674 struct mpd_message *message = mpd_recv_message(connection);
675 if (message == NULL &&
676 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
677 mpdclient_handle_error(c);
679 return message;
680 }
682 /****************************************************************************/
683 /*** Playlist management functions ******************************************/
684 /****************************************************************************/
686 /* update playlist */
687 bool
688 mpdclient_playlist_update(struct mpdclient *c)
689 {
690 struct mpd_connection *connection = mpdclient_get_connection(c);
691 if (connection == NULL)
692 return false;
694 playlist_clear(&c->playlist);
696 mpd_send_list_queue_meta(connection);
698 struct mpd_entity *entity;
699 while ((entity = mpd_recv_entity(connection))) {
700 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
701 playlist_append(&c->playlist, mpd_entity_get_song(entity));
703 mpd_entity_free(entity);
704 }
706 c->playlist.version = mpd_status_get_queue_version(c->status);
707 c->song = NULL;
709 return mpdclient_finish_command(c);
710 }
712 /* update playlist (plchanges) */
713 bool
714 mpdclient_playlist_update_changes(struct mpdclient *c)
715 {
716 struct mpd_connection *connection = mpdclient_get_connection(c);
718 if (connection == NULL)
719 return false;
721 mpd_send_queue_changes_meta(connection, c->playlist.version);
723 struct mpd_song *song;
724 while ((song = mpd_recv_song(connection)) != NULL) {
725 int pos = mpd_song_get_pos(song);
727 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
728 /* update song */
729 playlist_replace(&c->playlist, pos, song);
730 } else {
731 /* add a new song */
732 playlist_append(&c->playlist, song);
733 }
735 mpd_song_free(song);
736 }
738 /* remove trailing songs */
740 unsigned length = mpd_status_get_queue_length(c->status);
741 while (length < c->playlist.list->len) {
742 guint pos = c->playlist.list->len - 1;
744 /* Remove the last playlist entry */
745 playlist_remove(&c->playlist, pos);
746 }
748 c->song = NULL;
749 c->playlist.version = mpd_status_get_queue_version(c->status);
751 return mpdclient_finish_command(c);
752 }
755 /****************************************************************************/
756 /*** Filelist functions *****************************************************/
757 /****************************************************************************/
759 bool
760 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
761 {
762 struct mpd_connection *connection = mpdclient_get_connection(c);
763 if (connection == NULL)
764 return false;
766 if (filelist_is_empty(fl))
767 return true;
769 mpd_command_list_begin(connection, false);
771 for (unsigned i = 0; i < filelist_length(fl); ++i) {
772 struct filelist_entry *entry = filelist_get(fl, i);
773 struct mpd_entity *entity = entry->entity;
775 if (entity != NULL &&
776 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
777 const struct mpd_song *song =
778 mpd_entity_get_song(entity);
780 mpd_send_add(connection, mpd_song_get_uri(song));
781 }
782 }
784 mpd_command_list_end(connection);
785 return mpdclient_finish_command(c);
786 }