Code

mpdclient: added function mpdclient_cmd_move()
[ncmpc.git] / src / mpdclient.c
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;
106 void
107 mpdclient_free(struct mpdclient *c)
109         mpdclient_disconnect(c);
111         mpdclient_playlist_free(&c->playlist);
113         g_free(c);
116 void
117 mpdclient_disconnect(struct mpdclient *c)
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;
144 bool
145 mpdclient_connect(struct mpdclient *c,
146                   const gchar *host,
147                   gint port,
148                   gfloat _timeout,
149                   const gchar *password)
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;
176 bool
177 mpdclient_update(struct mpdclient *c)
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;
238 struct mpd_connection *
239 mpdclient_get_connection(struct mpdclient *c)
241         if (c->source != NULL && c->idle) {
242                 c->idle = false;
243                 mpd_glib_leave(c->source);
244         }
246         return c->connection;
249 void
250 mpdclient_put_connection(struct mpdclient *c)
252         assert(c->source == NULL || c->connection != NULL);
254         if (c->source != NULL && !c->idle) {
255                 c->idle = mpd_glib_enter(c->source);
256         }
259 static struct mpd_status *
260 mpdclient_recv_status(struct mpdclient *c)
262         struct mpd_status *status;
264         assert(c->connection != NULL);
266         status = mpd_recv_status(c->connection);
267         if (status == NULL) {
268                 mpdclient_handle_error(c);
269                 return NULL;
270         }
272         if (c->status != NULL)
273                 mpd_status_free(c->status);
274         return c->status = status;
277 /****************************************************************************/
278 /*** MPD Commands  **********************************************************/
279 /****************************************************************************/
281 bool
282 mpdclient_cmd_crop(struct mpdclient *c)
284         struct mpd_connection *connection;
285         int length, current;
287         if (c->status == NULL)
288                 return false;
290         length = mpd_status_get_queue_length(c->status);
291         current = mpd_status_get_song_pos(c->status);
292         if (current < 0 ||
293             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
294              mpd_status_get_state(c->status) != MPD_STATE_PAUSE) ||
295             mpd_status_get_queue_length(c->status) < 2)
296                 return true;
298         connection = mpdclient_get_connection(c);
299         if (connection == NULL)
300                 return false;
302         mpd_command_list_begin(connection, false);
304         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
305                 if (current < length - 1)
306                         mpd_send_delete_range(connection, current + 1, length);
307                 if (current > 0)
308                         mpd_send_delete_range(connection, 0, current);
309         } else
310                 while (--length >= 0)
311                         if (length != current)
312                                 mpd_send_delete(connection, length);
314         mpd_command_list_end(connection);
316         return mpdclient_finish_command(c);
319 bool
320 mpdclient_cmd_clear(struct mpdclient *c)
322         struct mpd_connection *connection = mpdclient_get_connection(c);
323         struct mpd_status *status;
325         if (connection == NULL)
326                 return false;
328         /* send "clear" and "status" */
329         if (!mpd_command_list_begin(connection, false) ||
330             !mpd_send_clear(connection) ||
331             !mpd_send_status(connection) ||
332             !mpd_command_list_end(connection))
333                 return mpdclient_handle_error(c);
335         /* receive the new status, store it in the mpdclient struct */
337         status = mpdclient_recv_status(c);
338         if (status == NULL)
339                 return false;
341         if (c->status != NULL)
342                 mpd_status_free(c->status);
343         c->status = status;
345         if (!mpd_response_finish(connection))
346                 return mpdclient_handle_error(c);
348         /* update mpdclient.playlist */
350         if (mpd_status_get_queue_length(status) == 0) {
351                 /* after the "clear" command, the queue is really
352                    empty - this means we can clear it locally,
353                    reducing the UI latency */
354                 playlist_clear(&c->playlist);
355                 c->playlist.version = mpd_status_get_queue_version(status);
356         }
358         c->events |= MPD_IDLE_QUEUE;
359         return true;
362 bool
363 mpdclient_cmd_volume(struct mpdclient *c, gint value)
365         struct mpd_connection *connection = mpdclient_get_connection(c);
366         if (connection == NULL)
367                 return false;
369         mpd_send_set_volume(connection, value);
370         return mpdclient_finish_command(c);
373 bool
374 mpdclient_cmd_volume_up(struct mpdclient *c)
376         struct mpd_connection *connection = mpdclient_get_connection(c);
377         if (connection == NULL)
378                 return false;
380         if (c->status == NULL ||
381             mpd_status_get_volume(c->status) == -1)
382                 return true;
384         if (c->volume < 0)
385                 c->volume = mpd_status_get_volume(c->status);
387         if (c->volume >= 100)
388                 return true;
390         return mpdclient_cmd_volume(c, ++c->volume);
393 bool
394 mpdclient_cmd_volume_down(struct mpdclient *c)
396         struct mpd_connection *connection = mpdclient_get_connection(c);
397         if (connection == NULL)
398                 return false;
400         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
401                 return true;
403         if (c->volume < 0)
404                 c->volume = mpd_status_get_volume(c->status);
406         if (c->volume <= 0)
407                 return true;
409         return mpdclient_cmd_volume(c, --c->volume);
412 bool
413 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
415         struct mpd_connection *connection = mpdclient_get_connection(c);
416         if (connection == NULL)
417                 return false;
419         mpd_send_add(connection, path_utf8);
420         return mpdclient_finish_command(c);
423 bool
424 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
426         struct mpd_connection *connection = mpdclient_get_connection(c);
427         struct mpd_status *status;
428         struct mpd_song *new_song;
430         assert(c != NULL);
431         assert(song != NULL);
433         if (connection == NULL || c->status == NULL)
434                 return false;
436         /* send the add command to mpd; at the same time, get the new
437            status (to verify the new playlist id) and the last song
438            (we hope that's the song we just added) */
440         if (!mpd_command_list_begin(connection, true) ||
441             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
442             !mpd_send_status(connection) ||
443             !mpd_send_get_queue_song_pos(connection,
444                                          playlist_length(&c->playlist)) ||
445             !mpd_command_list_end(connection) ||
446             !mpd_response_next(connection))
447                 return mpdclient_handle_error(c);
449         c->events |= MPD_IDLE_PLAYLIST;
451         status = mpdclient_recv_status(c);
452         if (status == NULL)
453                 return false;
455         if (!mpd_response_next(connection))
456                 return mpdclient_handle_error(c);
458         new_song = mpd_recv_song(connection);
459         if (!mpd_response_finish(connection) || new_song == NULL) {
460                 if (new_song != NULL)
461                         mpd_song_free(new_song);
463                 return mpd_connection_clear_error(connection) ||
464                         mpdclient_handle_error(c);
465         }
467         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
468             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
469                 /* the cheap route: match on the new playlist length
470                    and its version, we can keep our local playlist
471                    copy in sync */
472                 c->playlist.version = mpd_status_get_queue_version(status);
474                 /* the song we just received has the correct id;
475                    append it to the local playlist */
476                 playlist_append(&c->playlist, new_song);
477         }
479         mpd_song_free(new_song);
481         return true;
484 bool
485 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
487         struct mpd_connection *connection = mpdclient_get_connection(c);
488         const struct mpd_song *song;
489         struct mpd_status *status;
491         if (connection == NULL || c->status == NULL)
492                 return false;
494         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
495                 return false;
497         song = playlist_get(&c->playlist, idx);
499         /* send the delete command to mpd; at the same time, get the
500            new status (to verify the playlist id) */
502         if (!mpd_command_list_begin(connection, false) ||
503             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
504             !mpd_send_status(connection) ||
505             !mpd_command_list_end(connection))
506                 return mpdclient_handle_error(c);
508         c->events |= MPD_IDLE_PLAYLIST;
510         status = mpdclient_recv_status(c);
511         if (status == NULL)
512                 return false;
514         if (!mpd_response_finish(connection))
515                 return mpdclient_handle_error(c);
517         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
518             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
519                 /* the cheap route: match on the new playlist length
520                    and its version, we can keep our local playlist
521                    copy in sync */
522                 c->playlist.version = mpd_status_get_queue_version(status);
524                 /* remove the song from the local playlist */
525                 playlist_remove(&c->playlist, idx);
527                 /* remove references to the song */
528                 if (c->song == song)
529                         c->song = NULL;
530         }
532         return true;
535 /**
536  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
537  * It emulates the "delete range" command with a list of simple
538  * "delete" commands.
539  */
540 static bool
541 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
542                                     unsigned start, unsigned end)
544         struct mpd_connection *connection = mpdclient_get_connection(c);
545         if (connection == NULL)
546                 return false;
548         if (!mpd_command_list_begin(connection, false))
549                 return mpdclient_handle_error(c);
551         for (; start < end; --end)
552                 mpd_send_delete(connection, start);
554         if (!mpd_command_list_end(connection) ||
555             !mpd_response_finish(connection))
556                 return mpdclient_handle_error(c);
558         return true;
561 bool
562 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
564         struct mpd_connection *connection;
565         struct mpd_status *status;
567         if (end == start + 1)
568                 /* if that's not really a range, we choose to use the
569                    safer "deleteid" version */
570                 return mpdclient_cmd_delete(c, start);
572         connection = mpdclient_get_connection(c);
573         if (connection == NULL)
574                 return false;
576         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
577                 return mpdclient_cmd_delete_range_fallback(c, start, end);
579         /* MPD 0.16 supports "delete" with a range argument */
581         /* send the delete 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_delete_range(connection, start, end) ||
586             !mpd_send_status(connection) ||
587             !mpd_command_list_end(connection))
588                 return mpdclient_handle_error(c);
590         c->events |= MPD_IDLE_PLAYLIST;
592         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) - (end - start) &&
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                 /* remove the song from the local playlist */
607                 while (end > start) {
608                         --end;
610                         /* remove references to the song */
611                         if (c->song == playlist_get(&c->playlist, end))
612                                 c->song = NULL;
614                         playlist_remove(&c->playlist, end);
615                 }
616         }
618         return true;
621 bool
622 mpdclient_cmd_swap(struct mpdclient *c, gint old_index, gint new_index)
624         struct mpd_connection *connection = mpdclient_get_connection(c);
625         const struct mpd_song *song1, *song2;
626         struct mpd_status *status;
628         if (connection == NULL)
629                 return false;
631         if (old_index == new_index || new_index < 0 ||
632             (guint)new_index >= c->playlist.list->len)
633                 return false;
635         song1 = playlist_get(&c->playlist, old_index);
636         song2 = playlist_get(&c->playlist, new_index);
638         /* send the delete command to mpd; at the same time, get the
639            new status (to verify the playlist id) */
641         if (!mpd_command_list_begin(connection, false) ||
642             !mpd_send_swap_id(connection, mpd_song_get_id(song1),
643                               mpd_song_get_id(song2)) ||
644             !mpd_send_status(connection) ||
645             !mpd_command_list_end(connection))
646                 return mpdclient_handle_error(c);
648         c->events |= MPD_IDLE_PLAYLIST;
650         status = mpdclient_recv_status(c);
651         if (status == NULL)
652                 return false;
654         if (!mpd_response_finish(connection))
655                 return mpdclient_handle_error(c);
657         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
658             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
659                 /* the cheap route: match on the new playlist length
660                    and its version, we can keep our local playlist
661                    copy in sync */
662                 c->playlist.version = mpd_status_get_queue_version(status);
664                 /* swap songs in the local playlist */
665                 playlist_swap(&c->playlist, old_index, new_index);
666         }
668         return true;
671 bool
672 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
674         struct mpd_connection *connection;
675         struct mpd_status *status;
677         if (dest_pos == src_pos)
678                 return true;
680         connection = mpdclient_get_connection(c);
681         if (connection == NULL)
682                 return false;
684         /* send the "move" command to MPD; at the same time, get the
685            new status (to verify the playlist id) */
687         if (!mpd_command_list_begin(connection, false) ||
688             !mpd_send_move(connection, src_pos, dest_pos) ||
689             !mpd_send_status(connection) ||
690             !mpd_command_list_end(connection))
691                 return mpdclient_handle_error(c);
693         c->events |= MPD_IDLE_QUEUE;
695         status = mpdclient_recv_status(c);
696         if (status == NULL)
697                 return false;
699         if (!mpd_response_finish(connection))
700                 return mpdclient_handle_error(c);
702         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
703             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
704                 /* the cheap route: match on the new playlist length
705                    and its version, we can keep our local playlist
706                    copy in sync */
707                 c->playlist.version = mpd_status_get_queue_version(status);
709                 /* swap songs in the local playlist */
710                 playlist_move(&c->playlist, dest_pos, src_pos);
711         }
713         return true;
717 /****************************************************************************/
718 /*** Playlist management functions ******************************************/
719 /****************************************************************************/
721 /* update playlist */
722 bool
723 mpdclient_playlist_update(struct mpdclient *c)
725         struct mpd_connection *connection = mpdclient_get_connection(c);
726         struct mpd_entity *entity;
728         if (connection == NULL)
729                 return false;
731         playlist_clear(&c->playlist);
733         mpd_send_list_queue_meta(connection);
734         while ((entity = mpd_recv_entity(connection))) {
735                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
736                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
738                 mpd_entity_free(entity);
739         }
741         c->playlist.version = mpd_status_get_queue_version(c->status);
742         c->song = NULL;
744         return mpdclient_finish_command(c);
747 /* update playlist (plchanges) */
748 bool
749 mpdclient_playlist_update_changes(struct mpdclient *c)
751         struct mpd_connection *connection = mpdclient_get_connection(c);
752         struct mpd_song *song;
753         guint length;
755         if (connection == NULL)
756                 return false;
758         mpd_send_queue_changes_meta(connection, c->playlist.version);
760         while ((song = mpd_recv_song(connection)) != NULL) {
761                 int pos = mpd_song_get_pos(song);
763                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
764                         /* update song */
765                         playlist_replace(&c->playlist, pos, song);
766                 } else {
767                         /* add a new song */
768                         playlist_append(&c->playlist, song);
769                 }
771                 mpd_song_free(song);
772         }
774         /* remove trailing songs */
776         length = mpd_status_get_queue_length(c->status);
777         while (length < c->playlist.list->len) {
778                 guint pos = c->playlist.list->len - 1;
780                 /* Remove the last playlist entry */
781                 playlist_remove(&c->playlist, pos);
782         }
784         c->song = NULL;
785         c->playlist.version = mpd_status_get_queue_version(c->status);
787         return mpdclient_finish_command(c);
791 /****************************************************************************/
792 /*** Filelist functions *****************************************************/
793 /****************************************************************************/
795 bool
796 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
798         struct mpd_connection *connection = mpdclient_get_connection(c);
799         guint i;
801         if (connection == NULL)
802                 return false;
804         if (filelist_is_empty(fl))
805                 return true;
807         mpd_command_list_begin(connection, false);
809         for (i = 0; i < filelist_length(fl); ++i) {
810                 struct filelist_entry *entry = filelist_get(fl, i);
811                 struct mpd_entity *entity  = entry->entity;
813                 if (entity != NULL &&
814                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
815                         const struct mpd_song *song =
816                                 mpd_entity_get_song(entity);
818                         mpd_send_add(connection, mpd_song_get_uri(song));
819                 }
820         }
822         mpd_command_list_end(connection);
823         return mpdclient_finish_command(c);