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(void)
102 {
103 struct mpdclient *c = g_new0(struct mpdclient, 1);
104 playlist_init(&c->playlist);
105 c->volume = -1;
106 c->events = 0;
107 c->playing = false;
109 return c;
110 }
112 void
113 mpdclient_free(struct mpdclient *c)
114 {
115 mpdclient_disconnect(c);
117 mpdclient_playlist_free(&c->playlist);
119 g_free(c);
120 }
122 static void
123 mpdclient_status_free(struct mpdclient *c)
124 {
125 if (c->status == NULL)
126 return;
128 mpd_status_free(c->status);
129 c->status = NULL;
131 c->volume = -1;
132 c->playing = false;
133 }
135 void
136 mpdclient_disconnect(struct mpdclient *c)
137 {
138 if (c->source != NULL) {
139 mpd_glib_free(c->source);
140 c->source = NULL;
141 c->idle = false;
142 }
144 if (c->connection) {
145 mpd_connection_free(c->connection);
146 ++c->connection_id;
147 }
148 c->connection = NULL;
150 mpdclient_status_free(c);
152 playlist_clear(&c->playlist);
154 if (c->song)
155 c->song = NULL;
157 /* everything has changed after a disconnect */
158 c->events |= MPD_IDLE_ALL;
159 }
161 bool
162 mpdclient_connect(struct mpdclient *c,
163 const gchar *host,
164 gint port,
165 unsigned timeout_ms,
166 const gchar *password)
167 {
168 /* close any open connection */
169 if (c->connection)
170 mpdclient_disconnect(c);
172 /* connect to MPD */
173 c->connection = mpd_connection_new(host, port, timeout_ms);
174 if (c->connection == NULL)
175 g_error("Out of memory");
177 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
178 mpdclient_handle_error(c);
179 mpdclient_disconnect(c);
180 return false;
181 }
183 /* send password */
184 if (password != NULL && !mpd_run_password(c->connection, password)) {
185 mpdclient_handle_error(c);
186 mpdclient_disconnect(c);
187 return false;
188 }
190 c->source = mpd_glib_new(c->connection,
191 mpdclient_gidle_callback, c);
193 ++c->connection_id;
195 return true;
196 }
198 bool
199 mpdclient_update(struct mpdclient *c)
200 {
201 struct mpd_connection *connection = mpdclient_get_connection(c);
203 if (connection == NULL)
204 return false;
206 /* free the old status */
207 mpdclient_status_free(c);
209 /* retrieve new status */
210 c->status = mpd_run_status(connection);
211 if (c->status == NULL)
212 return mpdclient_handle_error(c);
214 c->volume = mpd_status_get_volume(c->status);
215 c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
217 /* check if the playlist needs an update */
218 if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
219 bool retval;
221 if (!playlist_is_empty(&c->playlist))
222 retval = mpdclient_playlist_update_changes(c);
223 else
224 retval = mpdclient_playlist_update(c);
225 if (!retval)
226 return false;
227 }
229 /* update the current song */
230 if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
231 c->song = playlist_get_song(&c->playlist,
232 mpd_status_get_song_pos(c->status));
233 }
235 return true;
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 }
259 static struct mpd_status *
260 mpdclient_recv_status(struct mpdclient *c)
261 {
262 assert(c->connection != NULL);
264 struct mpd_status *status = mpd_recv_status(c->connection);
265 if (status == NULL) {
266 mpdclient_handle_error(c);
267 return NULL;
268 }
270 if (c->status != NULL)
271 mpd_status_free(c->status);
272 return c->status = status;
273 }
275 /****************************************************************************/
276 /*** MPD Commands **********************************************************/
277 /****************************************************************************/
279 bool
280 mpdclient_cmd_crop(struct mpdclient *c)
281 {
282 if (!mpdclient_is_playing(c))
283 return false;
285 int length = mpd_status_get_queue_length(c->status);
286 int current = mpd_status_get_song_pos(c->status);
287 if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
288 return true;
290 struct mpd_connection *connection = mpdclient_get_connection(c);
291 if (connection == NULL)
292 return false;
294 mpd_command_list_begin(connection, false);
296 if (current < length - 1)
297 mpd_send_delete_range(connection, current + 1, length);
298 if (current > 0)
299 mpd_send_delete_range(connection, 0, current);
301 mpd_command_list_end(connection);
303 return mpdclient_finish_command(c);
304 }
306 bool
307 mpdclient_cmd_clear(struct mpdclient *c)
308 {
309 struct mpd_connection *connection = mpdclient_get_connection(c);
310 if (connection == NULL)
311 return false;
313 /* send "clear" and "status" */
314 if (!mpd_command_list_begin(connection, false) ||
315 !mpd_send_clear(connection) ||
316 !mpd_send_status(connection) ||
317 !mpd_command_list_end(connection))
318 return mpdclient_handle_error(c);
320 /* receive the new status, store it in the mpdclient struct */
322 struct mpd_status *status = mpdclient_recv_status(c);
323 if (status == NULL)
324 return false;
326 if (!mpd_response_finish(connection))
327 return mpdclient_handle_error(c);
329 /* update mpdclient.playlist */
331 if (mpd_status_get_queue_length(status) == 0) {
332 /* after the "clear" command, the queue is really
333 empty - this means we can clear it locally,
334 reducing the UI latency */
335 playlist_clear(&c->playlist);
336 c->playlist.version = mpd_status_get_queue_version(status);
337 c->song = NULL;
338 }
340 c->events |= MPD_IDLE_QUEUE;
341 return true;
342 }
344 bool
345 mpdclient_cmd_volume(struct mpdclient *c, gint value)
346 {
347 struct mpd_connection *connection = mpdclient_get_connection(c);
348 if (connection == NULL)
349 return false;
351 mpd_send_set_volume(connection, value);
352 return mpdclient_finish_command(c);
353 }
355 bool
356 mpdclient_cmd_volume_up(struct mpdclient *c)
357 {
358 struct mpd_connection *connection = mpdclient_get_connection(c);
359 if (connection == NULL)
360 return false;
362 if (c->status == NULL ||
363 mpd_status_get_volume(c->status) == -1)
364 return true;
366 if (c->volume < 0)
367 c->volume = mpd_status_get_volume(c->status);
369 if (c->volume >= 100)
370 return true;
372 return mpdclient_cmd_volume(c, ++c->volume);
373 }
375 bool
376 mpdclient_cmd_volume_down(struct mpdclient *c)
377 {
378 struct mpd_connection *connection = mpdclient_get_connection(c);
379 if (connection == NULL)
380 return false;
382 if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
383 return true;
385 if (c->volume < 0)
386 c->volume = mpd_status_get_volume(c->status);
388 if (c->volume <= 0)
389 return true;
391 return mpdclient_cmd_volume(c, --c->volume);
392 }
394 bool
395 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
396 {
397 struct mpd_connection *connection = mpdclient_get_connection(c);
398 if (connection == NULL)
399 return false;
401 return mpd_send_add(connection, path_utf8)?
402 mpdclient_finish_command(c) : false;
403 }
405 bool
406 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
407 {
408 assert(c != NULL);
409 assert(song != NULL);
411 struct mpd_connection *connection = mpdclient_get_connection(c);
412 if (connection == NULL || c->status == NULL)
413 return false;
415 /* send the add command to mpd; at the same time, get the new
416 status (to verify the new playlist id) and the last song
417 (we hope that's the song we just added) */
419 if (!mpd_command_list_begin(connection, true) ||
420 !mpd_send_add(connection, mpd_song_get_uri(song)) ||
421 !mpd_send_status(connection) ||
422 !mpd_send_get_queue_song_pos(connection,
423 playlist_length(&c->playlist)) ||
424 !mpd_command_list_end(connection) ||
425 !mpd_response_next(connection))
426 return mpdclient_handle_error(c);
428 c->events |= MPD_IDLE_QUEUE;
430 struct mpd_status *status = mpdclient_recv_status(c);
431 if (status == NULL)
432 return false;
434 if (!mpd_response_next(connection))
435 return mpdclient_handle_error(c);
437 struct mpd_song *new_song = mpd_recv_song(connection);
438 if (!mpd_response_finish(connection) || new_song == NULL) {
439 if (new_song != NULL)
440 mpd_song_free(new_song);
442 return mpd_connection_clear_error(connection) ||
443 mpdclient_handle_error(c);
444 }
446 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
447 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
448 /* the cheap route: match on the new playlist length
449 and its version, we can keep our local playlist
450 copy in sync */
451 c->playlist.version = mpd_status_get_queue_version(status);
453 /* the song we just received has the correct id;
454 append it to the local playlist */
455 playlist_append(&c->playlist, new_song);
456 }
458 mpd_song_free(new_song);
460 return true;
461 }
463 bool
464 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
465 {
466 struct mpd_connection *connection = mpdclient_get_connection(c);
468 if (connection == NULL || c->status == NULL)
469 return false;
471 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
472 return false;
474 const struct mpd_song *song = playlist_get(&c->playlist, idx);
476 /* send the delete command to mpd; at the same time, get the
477 new status (to verify the playlist id) */
479 if (!mpd_command_list_begin(connection, false) ||
480 !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
481 !mpd_send_status(connection) ||
482 !mpd_command_list_end(connection))
483 return mpdclient_handle_error(c);
485 c->events |= MPD_IDLE_QUEUE;
487 struct mpd_status *status = mpdclient_recv_status(c);
488 if (status == NULL)
489 return false;
491 if (!mpd_response_finish(connection))
492 return mpdclient_handle_error(c);
494 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
495 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
496 /* the cheap route: match on the new playlist length
497 and its version, we can keep our local playlist
498 copy in sync */
499 c->playlist.version = mpd_status_get_queue_version(status);
501 /* remove the song from the local playlist */
502 playlist_remove(&c->playlist, idx);
504 /* remove references to the song */
505 if (c->song == song)
506 c->song = NULL;
507 }
509 return true;
510 }
512 bool
513 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
514 {
515 if (end == start + 1)
516 /* if that's not really a range, we choose to use the
517 safer "deleteid" version */
518 return mpdclient_cmd_delete(c, start);
520 struct mpd_connection *connection = mpdclient_get_connection(c);
521 if (connection == NULL)
522 return false;
524 /* send the delete command to mpd; at the same time, get the
525 new status (to verify the playlist id) */
527 if (!mpd_command_list_begin(connection, false) ||
528 !mpd_send_delete_range(connection, start, end) ||
529 !mpd_send_status(connection) ||
530 !mpd_command_list_end(connection))
531 return mpdclient_handle_error(c);
533 c->events |= MPD_IDLE_QUEUE;
535 struct mpd_status *status = mpdclient_recv_status(c);
536 if (status == NULL)
537 return false;
539 if (!mpd_response_finish(connection))
540 return mpdclient_handle_error(c);
542 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
543 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
544 /* the cheap route: match on the new playlist length
545 and its version, we can keep our local playlist
546 copy in sync */
547 c->playlist.version = mpd_status_get_queue_version(status);
549 /* remove the song from the local playlist */
550 while (end > start) {
551 --end;
553 /* remove references to the song */
554 if (c->song == playlist_get(&c->playlist, end))
555 c->song = NULL;
557 playlist_remove(&c->playlist, end);
558 }
559 }
561 return true;
562 }
564 bool
565 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
566 {
567 if (dest_pos == src_pos)
568 return true;
570 struct mpd_connection *connection = mpdclient_get_connection(c);
571 if (connection == NULL)
572 return false;
574 /* send the "move" command to MPD; at the same time, get the
575 new status (to verify the playlist id) */
577 if (!mpd_command_list_begin(connection, false) ||
578 !mpd_send_move(connection, src_pos, dest_pos) ||
579 !mpd_send_status(connection) ||
580 !mpd_command_list_end(connection))
581 return mpdclient_handle_error(c);
583 c->events |= MPD_IDLE_QUEUE;
585 struct mpd_status *status = mpdclient_recv_status(c);
586 if (status == NULL)
587 return false;
589 if (!mpd_response_finish(connection))
590 return mpdclient_handle_error(c);
592 if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
593 mpd_status_get_queue_version(status) == c->playlist.version + 1) {
594 /* the cheap route: match on the new playlist length
595 and its version, we can keep our local playlist
596 copy in sync */
597 c->playlist.version = mpd_status_get_queue_version(status);
599 /* swap songs in the local playlist */
600 playlist_move(&c->playlist, dest_pos, src_pos);
601 }
603 return true;
604 }
606 /* The client-to-client protocol (MPD 0.17.0) */
608 bool
609 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
610 {
611 struct mpd_connection *connection = mpdclient_get_connection(c);
613 if (connection == NULL)
614 return false;
616 if (!mpd_send_subscribe(connection, channel))
617 return mpdclient_handle_error(c);
619 return mpdclient_finish_command(c);
620 }
622 bool
623 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
624 {
625 struct mpd_connection *connection = mpdclient_get_connection(c);
626 if (connection == NULL)
627 return false;
629 if (!mpd_send_unsubscribe(connection, channel))
630 return mpdclient_handle_error(c);
632 return mpdclient_finish_command(c);
633 }
635 bool
636 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
637 const char *text)
638 {
639 struct mpd_connection *connection = mpdclient_get_connection(c);
640 if (connection == NULL)
641 return false;
643 if (!mpd_send_send_message(connection, channel, text))
644 return mpdclient_handle_error(c);
646 return mpdclient_finish_command(c);
647 }
649 bool
650 mpdclient_send_read_messages(struct mpdclient *c)
651 {
652 struct mpd_connection *connection = mpdclient_get_connection(c);
653 if (connection == NULL)
654 return false;
656 return mpd_send_read_messages(connection)?
657 true : mpdclient_handle_error(c);
658 }
660 struct mpd_message *
661 mpdclient_recv_message(struct mpdclient *c)
662 {
663 struct mpd_connection *connection = mpdclient_get_connection(c);
664 if (connection == NULL)
665 return false;
667 struct mpd_message *message = mpd_recv_message(connection);
668 if (message == NULL &&
669 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
670 mpdclient_handle_error(c);
672 return message;
673 }
675 /****************************************************************************/
676 /*** Playlist management functions ******************************************/
677 /****************************************************************************/
679 /* update playlist */
680 bool
681 mpdclient_playlist_update(struct mpdclient *c)
682 {
683 struct mpd_connection *connection = mpdclient_get_connection(c);
684 if (connection == NULL)
685 return false;
687 playlist_clear(&c->playlist);
689 mpd_send_list_queue_meta(connection);
691 struct mpd_entity *entity;
692 while ((entity = mpd_recv_entity(connection))) {
693 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
694 playlist_append(&c->playlist, mpd_entity_get_song(entity));
696 mpd_entity_free(entity);
697 }
699 c->playlist.version = mpd_status_get_queue_version(c->status);
700 c->song = NULL;
702 return mpdclient_finish_command(c);
703 }
705 /* update playlist (plchanges) */
706 bool
707 mpdclient_playlist_update_changes(struct mpdclient *c)
708 {
709 struct mpd_connection *connection = mpdclient_get_connection(c);
711 if (connection == NULL)
712 return false;
714 mpd_send_queue_changes_meta(connection, c->playlist.version);
716 struct mpd_song *song;
717 while ((song = mpd_recv_song(connection)) != NULL) {
718 int pos = mpd_song_get_pos(song);
720 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
721 /* update song */
722 playlist_replace(&c->playlist, pos, song);
723 } else {
724 /* add a new song */
725 playlist_append(&c->playlist, song);
726 }
728 mpd_song_free(song);
729 }
731 /* remove trailing songs */
733 unsigned length = mpd_status_get_queue_length(c->status);
734 while (length < c->playlist.list->len) {
735 guint pos = c->playlist.list->len - 1;
737 /* Remove the last playlist entry */
738 playlist_remove(&c->playlist, pos);
739 }
741 c->song = NULL;
742 c->playlist.version = mpd_status_get_queue_version(c->status);
744 return mpdclient_finish_command(c);
745 }
748 /****************************************************************************/
749 /*** Filelist functions *****************************************************/
750 /****************************************************************************/
752 bool
753 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
754 {
755 struct mpd_connection *connection = mpdclient_get_connection(c);
756 if (connection == NULL)
757 return false;
759 if (filelist_is_empty(fl))
760 return true;
762 mpd_command_list_begin(connection, false);
764 for (unsigned i = 0; i < filelist_length(fl); ++i) {
765 struct filelist_entry *entry = filelist_get(fl, i);
766 struct mpd_entity *entity = entry->entity;
768 if (entity != NULL &&
769 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
770 const struct mpd_song *song =
771 mpd_entity_get_song(entity);
773 mpd_send_add(connection, mpd_song_get_uri(song));
774 }
775 }
777 mpd_command_list_end(connection);
778 return mpdclient_finish_command(c);
779 }