Code

mpdclient: optimize crop with delete_range
[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         }
260 /****************************************************************************/
261 /*** MPD Commands  **********************************************************/
262 /****************************************************************************/
264 bool
265 mpdclient_cmd_crop(struct mpdclient *c)
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         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
288                 if (current < length - 1)
289                         mpd_send_delete_range(connection, current + 1, length);
290                 if (current > 0)
291                         mpd_send_delete_range(connection, 0, current);
292         } else
293                 while (--length >= 0)
294                         if (length != current)
295                                 mpd_send_delete(connection, length);
297         mpd_command_list_end(connection);
299         return mpdclient_finish_command(c);
302 bool
303 mpdclient_cmd_clear(struct mpdclient *c)
305         struct mpd_connection *connection = mpdclient_get_connection(c);
306         struct mpd_status *status;
308         if (connection == NULL)
309                 return false;
311         /* send "clear" and "status" */
312         if (!mpd_command_list_begin(connection, false) ||
313             !mpd_send_clear(connection) ||
314             !mpd_send_status(connection) ||
315             !mpd_command_list_end(connection))
316                 return mpdclient_handle_error(c);
318         /* receive the new status, store it in the mpdclient struct */
320         status = mpd_recv_status(connection);
321         if (status == NULL)
322                 return mpdclient_handle_error(c);
324         if (c->status != NULL)
325                 mpd_status_free(c->status);
326         c->status = status;
328         if (!mpd_response_finish(connection))
329                 return mpdclient_handle_error(c);
331         /* update mpdclient.playlist */
333         if (mpd_status_get_queue_length(status) == 0) {
334                 /* after the "clear" command, the queue is really
335                    empty - this means we can clear it locally,
336                    reducing the UI latency */
337                 playlist_clear(&c->playlist);
338                 c->playlist.version = mpd_status_get_queue_version(status);
339         }
341         c->events |= MPD_IDLE_QUEUE;
342         return true;
345 bool
346 mpdclient_cmd_volume(struct mpdclient *c, gint value)
348         struct mpd_connection *connection = mpdclient_get_connection(c);
349         if (connection == NULL)
350                 return false;
352         mpd_send_set_volume(connection, value);
353         return mpdclient_finish_command(c);
356 bool
357 mpdclient_cmd_volume_up(struct mpdclient *c)
359         struct mpd_connection *connection = mpdclient_get_connection(c);
360         if (connection == NULL)
361                 return false;
363         if (c->status == NULL ||
364             mpd_status_get_volume(c->status) == -1)
365                 return true;
367         if (c->volume < 0)
368                 c->volume = mpd_status_get_volume(c->status);
370         if (c->volume >= 100)
371                 return true;
373         return mpdclient_cmd_volume(c, ++c->volume);
376 bool
377 mpdclient_cmd_volume_down(struct mpdclient *c)
379         struct mpd_connection *connection = mpdclient_get_connection(c);
380         if (connection == NULL)
381                 return false;
383         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
384                 return true;
386         if (c->volume < 0)
387                 c->volume = mpd_status_get_volume(c->status);
389         if (c->volume <= 0)
390                 return true;
392         return mpdclient_cmd_volume(c, --c->volume);
395 bool
396 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
398         struct mpd_connection *connection = mpdclient_get_connection(c);
399         if (connection == NULL)
400                 return false;
402         mpd_send_add(connection, path_utf8);
403         return mpdclient_finish_command(c);
406 bool
407 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
409         struct mpd_connection *connection = mpdclient_get_connection(c);
410         struct mpd_status *status;
411         struct mpd_song *new_song;
413         assert(c != NULL);
414         assert(song != NULL);
416         if (connection == NULL || c->status == NULL)
417                 return false;
419         /* send the add command to mpd; at the same time, get the new
420            status (to verify the new playlist id) and the last song
421            (we hope that's the song we just added) */
423         if (!mpd_command_list_begin(connection, true) ||
424             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
425             !mpd_send_status(connection) ||
426             !mpd_send_get_queue_song_pos(connection,
427                                          playlist_length(&c->playlist)) ||
428             !mpd_command_list_end(connection) ||
429             !mpd_response_next(connection))
430                 return mpdclient_handle_error(c);
432         c->events |= MPD_IDLE_PLAYLIST;
434         status = mpd_recv_status(connection);
435         if (status != NULL) {
436                 if (c->status != NULL)
437                         mpd_status_free(c->status);
438                 c->status = status;
439         }
441         if (!mpd_response_next(connection))
442                 return mpdclient_handle_error(c);
444         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;
470 bool
471 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
473         struct mpd_connection *connection = mpdclient_get_connection(c);
474         const struct mpd_song *song;
475         struct mpd_status *status;
477         if (connection == NULL || c->status == NULL)
478                 return false;
480         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
481                 return false;
483         song = playlist_get(&c->playlist, idx);
485         /* send the delete command to mpd; at the same time, get the
486            new status (to verify the playlist id) */
488         if (!mpd_command_list_begin(connection, false) ||
489             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
490             !mpd_send_status(connection) ||
491             !mpd_command_list_end(connection))
492                 return mpdclient_handle_error(c);
494         c->events |= MPD_IDLE_PLAYLIST;
496         status = mpd_recv_status(connection);
497         if (status != NULL) {
498                 if (c->status != NULL)
499                         mpd_status_free(c->status);
500                 c->status = status;
501         }
503         if (!mpd_response_finish(connection))
504                 return mpdclient_handle_error(c);
506         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
507             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
508                 /* the cheap route: match on the new playlist length
509                    and its version, we can keep our local playlist
510                    copy in sync */
511                 c->playlist.version = mpd_status_get_queue_version(status);
513                 /* remove the song from the local playlist */
514                 playlist_remove(&c->playlist, idx);
516                 /* remove references to the song */
517                 if (c->song == song)
518                         c->song = NULL;
519         }
521         return true;
524 /**
525  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
526  * It emulates the "delete range" command with a list of simple
527  * "delete" commands.
528  */
529 static bool
530 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
531                                     unsigned start, unsigned end)
533         struct mpd_connection *connection = mpdclient_get_connection(c);
534         if (connection == NULL)
535                 return false;
537         if (!mpd_command_list_begin(connection, false))
538                 return mpdclient_handle_error(c);
540         for (; start < end; --end)
541                 mpd_send_delete(connection, start);
543         if (!mpd_command_list_end(connection) ||
544             !mpd_response_finish(connection))
545                 return mpdclient_handle_error(c);
547         return true;
550 bool
551 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
553         struct mpd_connection *connection;
554         struct mpd_status *status;
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         connection = mpdclient_get_connection(c);
562         if (connection == NULL)
563                 return false;
565         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
566                 return mpdclient_cmd_delete_range_fallback(c, start, end);
568         /* MPD 0.16 supports "delete" with a range argument */
570         /* send the delete command to mpd; at the same time, get the
571            new status (to verify the playlist id) */
573         if (!mpd_command_list_begin(connection, false) ||
574             !mpd_send_delete_range(connection, start, end) ||
575             !mpd_send_status(connection) ||
576             !mpd_command_list_end(connection))
577                 return mpdclient_handle_error(c);
579         c->events |= MPD_IDLE_PLAYLIST;
581         status = mpd_recv_status(connection);
582         if (status != NULL) {
583                 if (c->status != NULL)
584                         mpd_status_free(c->status);
585                 c->status = status;
586         }
588         if (!mpd_response_finish(connection))
589                 return mpdclient_handle_error(c);
591         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
592             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
593                 /* the cheap route: match on the new playlist length
594                    and its version, we can keep our local playlist
595                    copy in sync */
596                 c->playlist.version = mpd_status_get_queue_version(status);
598                 /* remove the song from the local playlist */
599                 while (end > start) {
600                         --end;
602                         /* remove references to the song */
603                         if (c->song == playlist_get(&c->playlist, end))
604                                 c->song = NULL;
606                         playlist_remove(&c->playlist, end);
607                 }
608         }
610         return true;
613 bool
614 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
616         struct mpd_connection *connection = mpdclient_get_connection(c);
617         const struct mpd_song *song1, *song2;
618         struct mpd_status *status;
620         if (connection == NULL)
621                 return false;
623         if (old_index == new_index || new_index < 0 ||
624             (guint)new_index >= c->playlist.list->len)
625                 return false;
627         song1 = playlist_get(&c->playlist, old_index);
628         song2 = playlist_get(&c->playlist, new_index);
630         /* send the delete command to mpd; at the same time, get the
631            new status (to verify the playlist id) */
633         if (!mpd_command_list_begin(connection, false) ||
634             !mpd_send_swap_id(connection, mpd_song_get_id(song1),
635                               mpd_song_get_id(song2)) ||
636             !mpd_send_status(connection) ||
637             !mpd_command_list_end(connection))
638                 return mpdclient_handle_error(c);
640         c->events |= MPD_IDLE_PLAYLIST;
642         status = mpd_recv_status(connection);
643         if (status != NULL) {
644                 if (c->status != NULL)
645                         mpd_status_free(c->status);
646                 c->status = status;
647         }
649         if (!mpd_response_finish(connection))
650                 return mpdclient_handle_error(c);
652         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
653             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
654                 /* the cheap route: match on the new playlist length
655                    and its version, we can keep our local playlist
656                    copy in sync */
657                 c->playlist.version = mpd_status_get_queue_version(status);
659                 /* swap songs in the local playlist */
660                 playlist_swap(&c->playlist, old_index, new_index);
661         }
663         return true;
667 /****************************************************************************/
668 /*** Playlist management functions ******************************************/
669 /****************************************************************************/
671 /* update playlist */
672 bool
673 mpdclient_playlist_update(struct mpdclient *c)
675         struct mpd_connection *connection = mpdclient_get_connection(c);
676         struct mpd_entity *entity;
678         if (connection == NULL)
679                 return false;
681         playlist_clear(&c->playlist);
683         mpd_send_list_queue_meta(connection);
684         while ((entity = mpd_recv_entity(connection))) {
685                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
686                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
688                 mpd_entity_free(entity);
689         }
691         c->playlist.version = mpd_status_get_queue_version(c->status);
692         c->song = NULL;
694         return mpdclient_finish_command(c);
697 /* update playlist (plchanges) */
698 bool
699 mpdclient_playlist_update_changes(struct mpdclient *c)
701         struct mpd_connection *connection = mpdclient_get_connection(c);
702         struct mpd_song *song;
703         guint length;
705         if (connection == NULL)
706                 return false;
708         mpd_send_queue_changes_meta(connection, c->playlist.version);
710         while ((song = mpd_recv_song(connection)) != NULL) {
711                 int pos = mpd_song_get_pos(song);
713                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
714                         /* update song */
715                         playlist_replace(&c->playlist, pos, song);
716                 } else {
717                         /* add a new song */
718                         playlist_append(&c->playlist, song);
719                 }
721                 mpd_song_free(song);
722         }
724         /* remove trailing songs */
726         length = mpd_status_get_queue_length(c->status);
727         while (length < c->playlist.list->len) {
728                 guint pos = c->playlist.list->len - 1;
730                 /* Remove the last playlist entry */
731                 playlist_remove(&c->playlist, pos);
732         }
734         c->song = NULL;
735         c->playlist.version = mpd_status_get_queue_version(c->status);
737         return mpdclient_finish_command(c);
741 /****************************************************************************/
742 /*** Filelist functions *****************************************************/
743 /****************************************************************************/
745 bool
746 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
748         struct mpd_connection *connection = mpdclient_get_connection(c);
749         guint i;
751         if (connection == NULL)
752                 return false;
754         if (filelist_is_empty(fl))
755                 return true;
757         mpd_command_list_begin(connection, false);
759         for (i = 0; i < filelist_length(fl); ++i) {
760                 struct filelist_entry *entry = filelist_get(fl, i);
761                 struct mpd_entity *entity  = entry->entity;
763                 if (entity != NULL &&
764                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
765                         const struct mpd_song *song =
766                                 mpd_entity_get_song(entity);
768                         mpd_send_add(connection, mpd_song_get_uri(song));
769                 }
770         }
772         mpd_command_list_end(connection);
773         return mpdclient_finish_command(c);