73bb85f337c5679092e07e7526066fa36861676b
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 gboolean
32 mpdclient_enter_idle_callback(gpointer user_data)
33 {
34 struct mpdclient *c = user_data;
35 assert(c->enter_idle_source_id != 0);
36 assert(c->source != NULL);
37 assert(!c->idle);
39 c->enter_idle_source_id = 0;
40 c->idle = mpd_glib_enter(c->source);
41 return false;
42 }
44 static void
45 mpdclient_schedule_enter_idle(struct mpdclient *c)
46 {
47 assert(c != NULL);
48 assert(c->source != NULL);
50 if (c->enter_idle_source_id == 0)
51 /* automatically re-enter MPD "idle" mode */
52 c->enter_idle_source_id =
53 g_idle_add(mpdclient_enter_idle_callback, c);
54 }
56 static void
57 mpdclient_cancel_enter_idle(struct mpdclient *c)
58 {
59 if (c->enter_idle_source_id != 0) {
60 g_source_remove(c->enter_idle_source_id);
61 c->enter_idle_source_id = 0;
62 }
63 }
65 static void
66 mpdclient_invoke_error_callback(enum mpd_error error,
67 const char *message)
68 {
69 char *allocated;
70 if (error == MPD_ERROR_SERVER)
71 /* server errors are UTF-8, the others are locale */
72 message = allocated = utf8_to_locale(message);
73 else
74 allocated = NULL;
76 mpdclient_error_callback(message);
77 g_free(allocated);
78 }
80 static void
81 mpdclient_invoke_error_callback1(struct mpdclient *c)
82 {
83 assert(c != NULL);
84 assert(c->connection != NULL);
86 struct mpd_connection *connection = c->connection;
88 enum mpd_error error = mpd_connection_get_error(connection);
89 assert(error != MPD_ERROR_SUCCESS);
91 mpdclient_invoke_error_callback(error,
92 mpd_connection_get_error_message(connection));
93 }
95 static void
96 mpdclient_gidle_callback(enum mpd_error error,
97 gcc_unused enum mpd_server_error server_error,
98 const char *message, enum mpd_idle events,
99 void *ctx)
100 {
101 struct mpdclient *c = ctx;
103 c->idle = false;
105 assert(mpdclient_is_connected(c));
107 if (error != MPD_ERROR_SUCCESS) {
108 mpdclient_invoke_error_callback(error, message);
109 mpdclient_disconnect(c);
110 mpdclient_lost_callback();
111 return;
112 }
114 c->events |= events;
115 mpdclient_update(c);
117 mpdclient_idle_callback(c->events);
119 c->events = 0;
121 if (c->source != NULL)
122 mpdclient_schedule_enter_idle(c);
123 }
125 /****************************************************************************/
126 /*** mpdclient functions ****************************************************/
127 /****************************************************************************/
129 bool
130 mpdclient_handle_error(struct mpdclient *c)
131 {
132 enum mpd_error error = mpd_connection_get_error(c->connection);
134 assert(error != MPD_ERROR_SUCCESS);
136 if (error == MPD_ERROR_SERVER &&
137 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
138 mpdclient_auth_callback(c))
139 return true;
141 mpdclient_invoke_error_callback(error,
142 mpd_connection_get_error_message(c->connection));
144 if (!mpd_connection_clear_error(c->connection)) {
145 mpdclient_disconnect(c);
146 mpdclient_lost_callback();
147 }
149 return false;
150 }
152 struct mpdclient *
153 mpdclient_new(const gchar *host, unsigned port,
154 unsigned timeout_ms, const gchar *password)
155 {
156 struct mpdclient *c = g_new0(struct mpdclient, 1);
158 c->host = host;
159 c->port = port;
160 c->timeout_ms = timeout_ms;
161 c->password = password;
163 playlist_init(&c->playlist);
164 c->volume = -1;
165 c->events = 0;
166 c->playing = false;
168 return c;
169 }
171 void
172 mpdclient_free(struct mpdclient *c)
173 {
174 mpdclient_disconnect(c);
176 mpdclient_playlist_free(&c->playlist);
178 g_free(c);
179 }
181 static void
182 mpdclient_status_free(struct mpdclient *c)
183 {
184 if (c->status == NULL)
185 return;
187 mpd_status_free(c->status);
188 c->status = NULL;
190 c->volume = -1;
191 c->playing = false;
192 }
194 void
195 mpdclient_disconnect(struct mpdclient *c)
196 {
197 mpdclient_cancel_enter_idle(c);
199 if (c->source != NULL) {
200 mpd_glib_free(c->source);
201 c->source = NULL;
202 c->idle = false;
203 }
205 if (c->connection) {
206 mpd_connection_free(c->connection);
207 ++c->connection_id;
208 }
209 c->connection = NULL;
211 mpdclient_status_free(c);
213 playlist_clear(&c->playlist);
215 if (c->song)
216 c->song = NULL;
218 /* everything has changed after a disconnect */
219 c->events |= MPD_IDLE_ALL;
220 }
222 bool
223 mpdclient_connect(struct mpdclient *c)
224 {
225 /* close any open connection */
226 mpdclient_disconnect(c);
228 /* connect to MPD */
229 c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms);
230 if (c->connection == NULL)
231 g_error("Out of memory");
233 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
234 mpdclient_invoke_error_callback1(c);
235 mpdclient_disconnect(c);
236 mpdclient_failed_callback();
237 return false;
238 }
240 /* send password */
241 if (c->password != NULL &&
242 !mpd_run_password(c->connection, c->password)) {
243 mpdclient_invoke_error_callback1(c);
244 mpdclient_disconnect(c);
245 mpdclient_failed_callback();
246 return false;
247 }
249 c->source = mpd_glib_new(c->connection,
250 mpdclient_gidle_callback, c);
251 mpdclient_schedule_enter_idle(c);
253 ++c->connection_id;
255 mpdclient_connected_callback();
257 return true;
258 }
260 bool
261 mpdclient_update(struct mpdclient *c)
262 {
263 struct mpd_connection *connection = mpdclient_get_connection(c);
265 if (connection == NULL)
266 return false;
268 /* free the old status */
269 mpdclient_status_free(c);
271 /* retrieve new status */
272 c->status = mpd_run_status(connection);
273 if (c->status == NULL)
274 return mpdclient_handle_error(c);
276 c->volume = mpd_status_get_volume(c->status);
277 c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
279 /* check if the playlist needs an update */
280 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
281 bool retval;
283 if (!playlist_is_empty(&c->playlist))
284 retval = mpdclient_playlist_update_changes(c);
285 else
286 retval = mpdclient_playlist_update(c);
287 if (!retval)
288 return false;
289 }
291 /* update the current song */
292 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
293 c->song = playlist_get_song(&c->playlist,
294 mpd_status_get_song_pos(c->status));
295 }
297 return true;
298 }
300 struct mpd_connection *
301 mpdclient_get_connection(struct mpdclient *c)
302 {
303 if (c->source != NULL && c->idle) {
304 c->idle = false;
305 mpd_glib_leave(c->source);
307 mpdclient_schedule_enter_idle(c);
308 }
310 return c->connection;
311 }
313 static struct mpd_status *
314 mpdclient_recv_status(struct mpdclient *c)
315 {
316 assert(c->connection != NULL);
318 struct mpd_status *status = mpd_recv_status(c->connection);
319 if (status == NULL) {
320 mpdclient_handle_error(c);
321 return NULL;
322 }
324 if (c->status != NULL)
325 mpd_status_free(c->status);
326 return c->status = status;
327 }
329 /****************************************************************************/
330 /*** MPD Commands **********************************************************/
331 /****************************************************************************/
333 bool
334 mpdclient_cmd_crop(struct mpdclient *c)
335 {
336 if (!mpdclient_is_playing(c))
337 return false;
339 int length = mpd_status_get_queue_length(c->status);
340 int current = mpd_status_get_song_pos(c->status);
341 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
342 return true;
344 struct mpd_connection *connection = mpdclient_get_connection(c);
345 if (connection == NULL)
346 return false;
348 mpd_command_list_begin(connection, false);
350 if (current < length - 1)
351 mpd_send_delete_range(connection, current + 1, length);
352 if (current > 0)
353 mpd_send_delete_range(connection, 0, current);
355 mpd_command_list_end(connection);
357 return mpdclient_finish_command(c);
358 }
360 bool
361 mpdclient_cmd_clear(struct mpdclient *c)
362 {
363 struct mpd_connection *connection = mpdclient_get_connection(c);
364 if (connection == NULL)
365 return false;
367 /* send "clear" and "status" */
368 if (!mpd_command_list_begin(connection, false) ||
369 !mpd_send_clear(connection) ||
370 !mpd_send_status(connection) ||
371 !mpd_command_list_end(connection))
372 return mpdclient_handle_error(c);
374 /* receive the new status, store it in the mpdclient struct */
376 struct mpd_status *status = mpdclient_recv_status(c);
377 if (status == NULL)
378 return false;
380 if (!mpd_response_finish(connection))
381 return mpdclient_handle_error(c);
383 /* update mpdclient.playlist */
385 if (mpd_status_get_queue_length(status) == 0) {
386 /* after the "clear" command, the queue is really
387 empty - this means we can clear it locally,
388 reducing the UI latency */
389 playlist_clear(&c->playlist);
390 c->playlist.version = mpd_status_get_queue_version(status);
391 c->song = NULL;
392 }
394 c->events |= MPD_IDLE_QUEUE;
395 return true;
396 }
398 bool
399 mpdclient_cmd_volume(struct mpdclient *c, gint value)
400 {
401 struct mpd_connection *connection = mpdclient_get_connection(c);
402 if (connection == NULL)
403 return false;
405 mpd_send_set_volume(connection, value);
406 return mpdclient_finish_command(c);
407 }
409 bool
410 mpdclient_cmd_volume_up(struct mpdclient *c)
411 {
412 if (c->volume < 0 || c->volume >= 100)
413 return true;
415 struct mpd_connection *connection = mpdclient_get_connection(c);
416 if (connection == NULL)
417 return false;
419 return mpdclient_cmd_volume(c, ++c->volume);
420 }
422 bool
423 mpdclient_cmd_volume_down(struct mpdclient *c)
424 {
425 if (c->volume <= 0)
426 return true;
428 struct mpd_connection *connection = mpdclient_get_connection(c);
429 if (connection == NULL)
430 return false;
432 return mpdclient_cmd_volume(c, --c->volume);
433 }
435 bool
436 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
437 {
438 struct mpd_connection *connection = mpdclient_get_connection(c);
439 if (connection == NULL)
440 return false;
442 return mpd_send_add(connection, path_utf8)?
443 mpdclient_finish_command(c) : false;
444 }
446 bool
447 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
448 {
449 assert(c != NULL);
450 assert(song != NULL);
452 struct mpd_connection *connection = mpdclient_get_connection(c);
453 if (connection == NULL || c->status == NULL)
454 return false;
456 /* send the add command to mpd; at the same time, get the new
457 status (to verify the new playlist id) and the last song
458 (we hope that's the song we just added) */
460 if (!mpd_command_list_begin(connection, true) ||
461 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
462 !mpd_send_status(connection) ||
463 !mpd_send_get_queue_song_pos(connection,
464 playlist_length(&c->playlist)) ||
465 !mpd_command_list_end(connection) ||
466 !mpd_response_next(connection))
467 return mpdclient_handle_error(c);
469 c->events |= MPD_IDLE_QUEUE;
471 struct mpd_status *status = mpdclient_recv_status(c);
472 if (status == NULL)
473 return false;
475 if (!mpd_response_next(connection))
476 return mpdclient_handle_error(c);
478 struct mpd_song *new_song = mpd_recv_song(connection);
479 if (!mpd_response_finish(connection) || new_song == NULL) {
480 if (new_song != NULL)
481 mpd_song_free(new_song);
483 return mpd_connection_clear_error(connection) ||
484 mpdclient_handle_error(c);
485 }
487 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
488 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
489 /* the cheap route: match on the new playlist length
490 and its version, we can keep our local playlist
491 copy in sync */
492 c->playlist.version = mpd_status_get_queue_version(status);
494 /* the song we just received has the correct id;
495 append it to the local playlist */
496 playlist_append(&c->playlist, new_song);
497 }
499 mpd_song_free(new_song);
501 return true;
502 }
504 bool
505 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
506 {
507 struct mpd_connection *connection = mpdclient_get_connection(c);
509 if (connection == NULL || c->status == NULL)
510 return false;
512 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
513 return false;
515 const struct mpd_song *song = playlist_get(&c->playlist, idx);
517 /* send the delete command to mpd; at the same time, get the
518 new status (to verify the playlist id) */
520 if (!mpd_command_list_begin(connection, false) ||
521 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
522 !mpd_send_status(connection) ||
523 !mpd_command_list_end(connection))
524 return mpdclient_handle_error(c);
526 c->events |= MPD_IDLE_QUEUE;
528 struct mpd_status *status = mpdclient_recv_status(c);
529 if (status == NULL)
530 return false;
532 if (!mpd_response_finish(connection))
533 return mpdclient_handle_error(c);
535 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
536 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
537 /* the cheap route: match on the new playlist length
538 and its version, we can keep our local playlist
539 copy in sync */
540 c->playlist.version = mpd_status_get_queue_version(status);
542 /* remove the song from the local playlist */
543 playlist_remove(&c->playlist, idx);
545 /* remove references to the song */
546 if (c->song == song)
547 c->song = NULL;
548 }
550 return true;
551 }
553 bool
554 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
555 {
556 if (end == start + 1)
557 /* if that's not really a range, we choose to use the
558 safer "deleteid" version */
559 return mpdclient_cmd_delete(c, start);
561 struct mpd_connection *connection = mpdclient_get_connection(c);
562 if (connection == NULL)
563 return false;
565 /* send the delete command to mpd; at the same time, get the
566 new status (to verify the playlist id) */
568 if (!mpd_command_list_begin(connection, false) ||
569 !mpd_send_delete_range(connection, start, end) ||
570 !mpd_send_status(connection) ||
571 !mpd_command_list_end(connection))
572 return mpdclient_handle_error(c);
574 c->events |= MPD_IDLE_QUEUE;
576 struct mpd_status *status = mpdclient_recv_status(c);
577 if (status == NULL)
578 return false;
580 if (!mpd_response_finish(connection))
581 return mpdclient_handle_error(c);
583 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
584 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
585 /* the cheap route: match on the new playlist length
586 and its version, we can keep our local playlist
587 copy in sync */
588 c->playlist.version = mpd_status_get_queue_version(status);
590 /* remove the song from the local playlist */
591 while (end > start) {
592 --end;
594 /* remove references to the song */
595 if (c->song == playlist_get(&c->playlist, end))
596 c->song = NULL;
598 playlist_remove(&c->playlist, end);
599 }
600 }
602 return true;
603 }
605 bool
606 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
607 {
608 if (dest_pos == src_pos)
609 return true;
611 struct mpd_connection *connection = mpdclient_get_connection(c);
612 if (connection == NULL)
613 return false;
615 /* send the "move" command to MPD; at the same time, get the
616 new status (to verify the playlist id) */
618 if (!mpd_command_list_begin(connection, false) ||
619 !mpd_send_move(connection, src_pos, dest_pos) ||
620 !mpd_send_status(connection) ||
621 !mpd_command_list_end(connection))
622 return mpdclient_handle_error(c);
624 c->events |= MPD_IDLE_QUEUE;
626 struct mpd_status *status = mpdclient_recv_status(c);
627 if (status == NULL)
628 return false;
630 if (!mpd_response_finish(connection))
631 return mpdclient_handle_error(c);
633 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
634 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
635 /* the cheap route: match on the new playlist length
636 and its version, we can keep our local playlist
637 copy in sync */
638 c->playlist.version = mpd_status_get_queue_version(status);
640 /* swap songs in the local playlist */
641 playlist_move(&c->playlist, dest_pos, src_pos);
642 }
644 return true;
645 }
647 /* The client-to-client protocol (MPD 0.17.0) */
649 bool
650 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
651 {
652 struct mpd_connection *connection = mpdclient_get_connection(c);
654 if (connection == NULL)
655 return false;
657 if (!mpd_send_subscribe(connection, channel))
658 return mpdclient_handle_error(c);
660 return mpdclient_finish_command(c);
661 }
663 bool
664 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
665 {
666 struct mpd_connection *connection = mpdclient_get_connection(c);
667 if (connection == NULL)
668 return false;
670 if (!mpd_send_unsubscribe(connection, channel))
671 return mpdclient_handle_error(c);
673 return mpdclient_finish_command(c);
674 }
676 bool
677 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
678 const char *text)
679 {
680 struct mpd_connection *connection = mpdclient_get_connection(c);
681 if (connection == NULL)
682 return false;
684 if (!mpd_send_send_message(connection, channel, text))
685 return mpdclient_handle_error(c);
687 return mpdclient_finish_command(c);
688 }
690 bool
691 mpdclient_send_read_messages(struct mpdclient *c)
692 {
693 struct mpd_connection *connection = mpdclient_get_connection(c);
694 if (connection == NULL)
695 return false;
697 return mpd_send_read_messages(connection)?
698 true : mpdclient_handle_error(c);
699 }
701 struct mpd_message *
702 mpdclient_recv_message(struct mpdclient *c)
703 {
704 struct mpd_connection *connection = mpdclient_get_connection(c);
705 if (connection == NULL)
706 return false;
708 struct mpd_message *message = mpd_recv_message(connection);
709 if (message == NULL &&
710 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
711 mpdclient_handle_error(c);
713 return message;
714 }
716 /****************************************************************************/
717 /*** Playlist management functions ******************************************/
718 /****************************************************************************/
720 /* update playlist */
721 bool
722 mpdclient_playlist_update(struct mpdclient *c)
723 {
724 struct mpd_connection *connection = mpdclient_get_connection(c);
725 if (connection == NULL)
726 return false;
728 playlist_clear(&c->playlist);
730 mpd_send_list_queue_meta(connection);
732 struct mpd_entity *entity;
733 while ((entity = mpd_recv_entity(connection))) {
734 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
735 playlist_append(&c->playlist, mpd_entity_get_song(entity));
737 mpd_entity_free(entity);
738 }
740 c->playlist.version = mpd_status_get_queue_version(c->status);
741 c->song = NULL;
743 return mpdclient_finish_command(c);
744 }
746 /* update playlist (plchanges) */
747 bool
748 mpdclient_playlist_update_changes(struct mpdclient *c)
749 {
750 struct mpd_connection *connection = mpdclient_get_connection(c);
752 if (connection == NULL)
753 return false;
755 mpd_send_queue_changes_meta(connection, c->playlist.version);
757 struct mpd_song *song;
758 while ((song = mpd_recv_song(connection)) != NULL) {
759 int pos = mpd_song_get_pos(song);
761 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
762 /* update song */
763 playlist_replace(&c->playlist, pos, song);
764 } else {
765 /* add a new song */
766 playlist_append(&c->playlist, song);
767 }
769 mpd_song_free(song);
770 }
772 /* remove trailing songs */
774 unsigned length = mpd_status_get_queue_length(c->status);
775 while (length < c->playlist.list->len) {
776 guint pos = c->playlist.list->len - 1;
778 /* Remove the last playlist entry */
779 playlist_remove(&c->playlist, pos);
780 }
782 c->song = NULL;
783 c->playlist.version = mpd_status_get_queue_version(c->status);
785 return mpdclient_finish_command(c);
786 }
789 /****************************************************************************/
790 /*** Filelist functions *****************************************************/
791 /****************************************************************************/
793 bool
794 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
795 {
796 struct mpd_connection *connection = mpdclient_get_connection(c);
797 if (connection == NULL)
798 return false;
800 if (filelist_is_empty(fl))
801 return true;
803 mpd_command_list_begin(connection, false);
805 for (unsigned i = 0; i < filelist_length(fl); ++i) {
806 struct filelist_entry *entry = filelist_get(fl, i);
807 struct mpd_entity *entity = entry->entity;
809 if (entity != NULL &&
810 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
811 const struct mpd_song *song =
812 mpd_entity_get_song(entity);
814 mpd_send_add(connection, mpd_song_get_uri(song));
815 }
816 }
818 mpd_command_list_end(connection);
819 return mpdclient_finish_command(c);
820 }