Code

screen_*: eliminate redundant wrefresh() calls
[ncmpc.git] / src / screen_queue.c
index 0381d52a0dbdeefb9fb0b2cbe0619feb2cc97360..5b3b5f74cf005528e11d90df42715fd71da55267 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -20,7 +20,7 @@
 #include "screen_queue.h"
 #include "screen_interface.h"
 #include "screen_file.h"
-#include "screen_message.h"
+#include "screen_status.h"
 #include "screen_find.h"
 #include "config.h"
 #include "i18n.h"
 #include "utils.h"
 #include "strfsong.h"
 #include "wreadln.h"
-#include "colors.h"
+#include "song_paint.h"
 #include "screen.h"
 #include "screen_utils.h"
 #include "screen_song.h"
 #include "screen_lyrics.h"
+#include "Compiler.h"
 
 #ifndef NCMPC_MINI
 #include "hscroll.h"
@@ -57,7 +58,6 @@ typedef struct
 } completion_callback_data_t;
 
 static struct hscroll hscroll;
-static guint scroll_source_id;
 #endif
 
 static struct mpdclient_playlist *playlist;
@@ -96,128 +96,53 @@ screen_queue_save_selection(void)
 static void
 screen_queue_restore_selection(void)
 {
-       const struct mpd_song *song;
-       int pos;
-
        list_window_set_length(lw, playlist_length(playlist));
 
        if (selected_song_id < 0)
                /* there was no selection */
                return;
 
-       song = screen_queue_selected_song();
+       const struct mpd_song *song = screen_queue_selected_song();
        if (song != NULL &&
            mpd_song_get_id(song) == (unsigned)selected_song_id)
                /* selection is still valid */
                return;
 
-       pos = playlist_get_index_from_id(playlist, selected_song_id);
+       int pos = playlist_get_index_from_id(playlist, selected_song_id);
        if (pos >= 0)
                list_window_set_cursor(lw, pos);
 
        screen_queue_save_selection();
 }
 
-#ifndef NCMPC_MINI
-static gboolean
-scroll_timer_callback(G_GNUC_UNUSED gpointer data)
-{
-       scroll_source_id = 0;
-
-       hscroll_step(&hscroll);
-       screen_queue_repaint();
-       return false;
-}
-#endif
-
 static const char *
-screen_queue_lw_callback(unsigned idx, bool *highlight, char **second_column,
-                        G_GNUC_UNUSED void *data)
+screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
 {
        static char songname[MAX_SONG_LENGTH];
-       struct mpd_song *song;
 
        assert(playlist != NULL);
        assert(idx < playlist_length(playlist));
 
-       song = playlist_get(playlist, idx);
-       if ((int)mpd_song_get_id(song) == current_song_id)
-               *highlight = true;
+       struct mpd_song *song = playlist_get(playlist, idx);
 
        strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
 
-#ifndef NCMPC_MINI
-       if (second_column != NULL && mpd_song_get_duration(song) > 0) {
-               char duration[32];
-               format_duration_short(duration, sizeof(duration),
-                                     mpd_song_get_duration(song));
-               *second_column = g_strdup(duration);
-       }
-
-       if (idx == lw->selected)
-       {
-               int second_column_len = 0;
-               if (second_column != NULL && *second_column != NULL)
-                       second_column_len = strlen(*second_column);
-               if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
-               {
-                       static unsigned current_song;
-                       char *tmp;
-
-                       if (current_song != lw->selected) {
-                               hscroll_reset(&hscroll);
-                               current_song = lw->selected;
-                       }
-
-                       tmp = strscroll(&hscroll, songname, options.scroll_sep,
-                                       MAX_SONG_LENGTH);
-                       g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
-                       g_free(tmp);
-
-                       if (scroll_source_id == 0)
-                               scroll_source_id =
-                                       g_timeout_add(1000,
-                                                     scroll_timer_callback,
-                                                     NULL);
-               } else {
-                       hscroll_reset(&hscroll);
-
-                       if (scroll_source_id != 0) {
-                               g_source_remove(scroll_source_id);
-                               scroll_source_id = 0;
-                       }
-               }
-       }
-#else
-       (void)second_column;
-#endif
-
        return songname;
 }
 
 static void
-center_playing_item(struct mpdclient *c, bool center_cursor)
+center_playing_item(const struct mpd_status *status, bool center_cursor)
 {
-       const struct mpd_song *song;
-       unsigned length = c->playlist.list->len;
-       int idx;
-
-       song = mpdclient_get_current_song(c);
-       if (song == NULL)
+       if (status == NULL ||
+           (mpd_status_get_state(status) != MPD_STATE_PLAY &&
+            mpd_status_get_state(status) != MPD_STATE_PAUSE))
                return;
 
        /* try to center the song that are playing */
-       idx = playlist_get_index(&c->playlist, c->song);
+       int idx = mpd_status_get_song_pos(status);
        if (idx < 0)
                return;
 
-       if (length < lw->rows)
-       {
-               if (center_cursor)
-                       list_window_set_cursor(lw, idx);
-               return;
-       }
-
        list_window_center(lw, idx);
 
        if (center_cursor) {
@@ -229,9 +154,35 @@ center_playing_item(struct mpdclient *c, bool center_cursor)
        list_window_fetch_cursor(lw);
 }
 
+gcc_pure
+static int
+get_current_song_id(const struct mpd_status *status)
+{
+       return status != NULL &&
+               (mpd_status_get_state(status) == MPD_STATE_PLAY ||
+                mpd_status_get_state(status) == MPD_STATE_PAUSE)
+               ? (int)mpd_status_get_song_id(status)
+               : -1;
+}
+
+static bool
+screen_queue_song_change(const struct mpd_status *status)
+{
+       if (get_current_song_id(status) == current_song_id)
+               return false;
+
+       current_song_id = get_current_song_id(status);
+
+       /* center the cursor */
+       if (options.auto_center && !lw->range_selection)
+               center_playing_item(status, false);
+
+       return true;
+}
+
 #ifndef NCMPC_MINI
 static void
-save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
+save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
                       void *data)
 {
        completion_callback_data_t *tmp = (completion_callback_data_t *)data;
@@ -246,9 +197,9 @@ save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
 }
 
 static void
-save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
-                       G_GNUC_UNUSED gchar *line, GList *items,
-                       G_GNUC_UNUSED void *data)
+save_post_completion_cb(gcc_unused GCompletion *gcmp,
+                       gcc_unused gchar *line, GList *items,
+                       gcc_unused void *data)
 {
        if (g_list_length(items) >= 1)
                screen_display_completion_list(items);
@@ -272,12 +223,7 @@ int
 playlist_save(struct mpdclient *c, char *name, char *defaultname)
 {
        struct mpd_connection *connection;
-       gchar *filename, *filename_utf8;
-#ifndef NCMPC_MINI
-       GCompletion *gcmp;
-       GList *list = NULL;
-       completion_callback_data_t data;
-#endif
+       gchar *filename;
 
 #ifdef NCMPC_MINI
        (void)defaultname;
@@ -286,18 +232,21 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname)
 #ifndef NCMPC_MINI
        if (name == NULL) {
                /* initialize completion support */
-               gcmp = g_completion_new(NULL);
+               GCompletion *gcmp = g_completion_new(NULL);
                g_completion_set_compare(gcmp, completion_strncmp);
-               data.list = &list;
-               data.dir_list = NULL;
-               data.c = c;
+               GList *list = NULL;
+               completion_callback_data_t data = {
+                       .list = &list,
+                       .dir_list = NULL,
+                       .c = c,
+               };
                wrln_completion_callback_data = &data;
                wrln_pre_completion_callback = save_pre_completion_cb;
                wrln_post_completion_callback = save_post_completion_cb;
 
 
                /* query the user for a filename */
-               filename = screen_readln(_("Save playlist as"),
+               filename = screen_readln(_("Save queue as"),
                                         defaultname,
                                         NULL,
                                         gcmp);
@@ -312,29 +261,30 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname)
                        filename=g_strstrip(filename);
        } else
 #endif
-                       filename=g_strdup(name);
+               filename=g_strdup(name);
 
        if (filename == NULL)
                return -1;
 
        /* send save command to mpd */
 
-       filename_utf8 = locale_to_utf8(filename);
-
        connection = mpdclient_get_connection(c);
+       if (connection == NULL) {
+               g_free(filename);
+               return -1;
+       }
+
+       char *filename_utf8 = locale_to_utf8(filename);
        if (!mpd_run_save(connection, filename_utf8)) {
                if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
                    mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
                    mpd_connection_clear_error(connection)) {
-                       char *buf;
-                       int key;
-
-                       buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
-                                             filename, YES, NO);
-                       key = tolower(screen_getch(buf));
+                       char *buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
+                                                   filename, YES, NO);
+                       bool replace = screen_get_yesno(buf, false);
                        g_free(buf);
 
-                       if (key != YES[0]) {
+                       if (!replace) {
                                g_free(filename_utf8);
                                g_free(filename);
                                screen_status_printf(_("Aborted"));
@@ -417,34 +367,31 @@ static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
 static int
 handle_add_to_playlist(struct mpdclient *c)
 {
-       gchar *path;
 #ifndef NCMPC_MINI
-       GCompletion *gcmp;
+       /* initialize completion support */
+       GCompletion *gcmp = g_completion_new(NULL);
+       g_completion_set_compare(gcmp, completion_strncmp);
+
        GList *list = NULL;
        GList *dir_list = NULL;
-       completion_callback_data_t data;
+       completion_callback_data_t data = {
+               .list = &list,
+               .dir_list = &dir_list,
+               .c = c,
+       };
 
-       /* initialize completion support */
-       gcmp = g_completion_new(NULL);
-       g_completion_set_compare(gcmp, completion_strncmp);
-       data.list = &list;
-       data.dir_list = &dir_list;
-       data.c = c;
        wrln_completion_callback_data = &data;
        wrln_pre_completion_callback = add_pre_completion_cb;
        wrln_post_completion_callback = add_post_completion_cb;
+#else
+       GCompletion *gcmp = NULL;
 #endif
 
        /* get path */
-       path = screen_readln(_("Add"),
-                            NULL,
-                            NULL,
-#ifdef NCMPC_MINI
-                            NULL
-#else
-                            gcmp
-#endif
-                            );
+       char *path = screen_readln(_("Add"),
+                                  NULL,
+                                  NULL,
+                                  gcmp);
 
        /* destroy completion data */
 #ifndef NCMPC_MINI
@@ -471,6 +418,11 @@ static void
 screen_queue_init(WINDOW *w, int cols, int rows)
 {
        lw = list_window_init(w, cols, rows);
+
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_init(&hscroll, w, options.scroll_sep);
+#endif
 }
 
 static gboolean
@@ -509,6 +461,7 @@ screen_queue_open(struct mpdclient *c)
        }
 
        screen_queue_restore_selection();
+       screen_queue_song_change(c->status);
 }
 
 static void
@@ -518,6 +471,11 @@ screen_queue_close(void)
                g_source_remove(timer_hide_cursor_id);
                timer_hide_cursor_id = 0;
        }
+
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_clear(&hscroll);
+#endif
 }
 
 static void
@@ -537,57 +495,71 @@ static const char *
 screen_queue_title(char *str, size_t size)
 {
        if (options.host == NULL)
-               return _("Playlist");
+               return _("Queue");
 
-       g_snprintf(str, size, _("Playlist on %s"), options.host);
+       g_snprintf(str, size, _("Queue on %s"), options.host);
        return str;
 }
 
+static void
+screen_queue_paint_callback(WINDOW *w, unsigned i,
+                           unsigned y, unsigned width,
+                           bool selected, gcc_unused const void *data)
+{
+       assert(playlist != NULL);
+       assert(i < playlist_length(playlist));
+
+       const struct mpd_song *song = playlist_get(playlist, i);
+
+       struct hscroll *row_hscroll = NULL;
+#ifndef NCMPC_MINI
+       row_hscroll = selected && options.scroll && lw->selected == i
+               ? &hscroll : NULL;
+#endif
+
+       paint_song_row(w, y, width, selected,
+                      (int)mpd_song_get_id(song) == current_song_id,
+                      song, row_hscroll, options.list_format);
+}
+
 static void
 screen_queue_paint(void)
 {
-       list_window_paint(lw, screen_queue_lw_callback, NULL);
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_clear(&hscroll);
+#endif
+
+       list_window_paint2(lw, screen_queue_paint_callback, NULL);
 }
 
 static void
 screen_queue_update(struct mpdclient *c)
 {
-       static int prev_song_id = -1;
-
-       if (c->events & MPD_IDLE_PLAYLIST)
+       if (c->events & MPD_IDLE_QUEUE)
                screen_queue_restore_selection();
-
-       current_song_id = c->status != NULL &&
-               (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
-                mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
-               ? (int)mpd_status_get_song_id(c->status) : -1;
-
-       if (current_song_id != prev_song_id) {
-               prev_song_id = current_song_id;
-
-               /* center the cursor */
-               if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
-                       center_playing_item(c, false);
-
-               screen_queue_repaint();
-       } else if (c->events & MPD_IDLE_PLAYLIST) {
-               /* the playlist has changed, we must paint the new
-                  version */
-               screen_queue_repaint();
-       }
+       else
+               /* the queue size may have changed, even if we havn't
+                  received the QUEUE idle event yet */
+               list_window_set_length(lw, playlist_length(playlist));
+
+       if (((c->events & MPD_IDLE_PLAYER) != 0 &&
+            screen_queue_song_change(c->status)) ||
+           c->events & MPD_IDLE_QUEUE)
+               /* the queue or the current song has changed, we must
+                  paint the new version */
+               screen_queue_paint();
 }
 
 #ifdef HAVE_GETMOUSE
 static bool
 handle_mouse_event(struct mpdclient *c)
 {
-       int row;
-       unsigned selected;
        unsigned long bstate;
-
+       int row;
        if (screen_get_mouse_event(c, &bstate, &row) ||
            list_window_mouse(lw, bstate, row)) {
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        }
 
@@ -597,21 +569,31 @@ handle_mouse_event(struct mpdclient *c)
                return true;
        }
 
-       selected = lw->start + row;
+       const unsigned old_selected = lw->selected;
+       list_window_set_cursor(lw, lw->start + row);
 
        if (bstate & BUTTON1_CLICKED) {
                /* play */
-               if (lw->start + row < playlist_length(playlist))
-                       mpdclient_cmd_play(c, lw->start + row);
+               const struct mpd_song *song = screen_queue_selected_song();
+               if (song != NULL) {
+                       struct mpd_connection *connection =
+                               mpdclient_get_connection(c);
+
+                       if (connection != NULL &&
+                           !mpd_run_play_id(connection,
+                                            mpd_song_get_id(song)))
+                               mpdclient_handle_error(c);
+               }
        } else if (bstate & BUTTON3_CLICKED) {
                /* delete */
-               if (selected == lw->selected)
+               if (lw->selected == old_selected)
                        mpdclient_cmd_delete(c, lw->selected);
+
+               list_window_set_length(lw, playlist_length(playlist));
        }
 
-       list_window_set_cursor(lw, selected);
        screen_queue_save_selection();
-       screen_queue_repaint();
+       screen_queue_paint();
 
        return true;
 }
@@ -622,9 +604,8 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 {
        struct mpd_connection *connection;
        static command_t cached_cmd = CMD_NONE;
-       command_t prev_cmd = cached_cmd;
-       struct list_window_range range;
 
+       const command_t prev_cmd = cached_cmd;
        cached_cmd = cmd;
 
        lw->hide_cursor = false;
@@ -638,20 +619,20 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        if (list_window_cmd(lw, cmd)) {
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        }
 
        switch(cmd) {
        case CMD_SCREEN_UPDATE:
-               center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
-               screen_queue_repaint();
+               center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
+               screen_queue_paint();
                return false;
        case CMD_SELECT_PLAYING:
                list_window_set_cursor(lw, playlist_get_index(&c->playlist,
                                                              c->song));
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
 
        case CMD_LIST_FIND:
@@ -660,12 +641,12 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND_NEXT:
                screen_find(lw, cmd, screen_queue_lw_callback, NULL);
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        case CMD_LIST_JUMP:
-               screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
+               screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
 
 #ifdef HAVE_GETMOUSE
@@ -701,7 +682,10 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
                break;
 #endif
        case CMD_SCREEN_SWAP:
-               screen_swap(c, playlist_get(&c->playlist, lw->selected));
+               if (playlist_length(&c->playlist) > 0)
+                       screen_swap(c, playlist_get(&c->playlist, lw->selected));
+               else
+                       screen_swap(c, NULL);
                return true;
 
        default:
@@ -712,8 +696,19 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
                return false;
 
        switch(cmd) {
+               const struct mpd_song *song;
+               struct list_window_range range;
+
        case CMD_PLAY:
-               mpdclient_cmd_play(c, lw->selected);
+               song = screen_queue_selected_song();
+               if (song == NULL)
+                       return false;
+
+               connection = mpdclient_get_connection(c);
+               if (connection != NULL &&
+                   !mpd_run_play_id(connection, mpd_song_get_id(song)))
+                       mpdclient_handle_error(c);
+
                return true;
 
        case CMD_DELETE:
@@ -733,13 +728,16 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        case CMD_SHUFFLE:
                list_window_get_range(lw, &range);
-               if (range.end < range.start + 1)
+               if (range.end <= range.start + 1)
                        /* No range selection, shuffle all list. */
                        break;
 
                connection = mpdclient_get_connection(c);
+               if (connection == NULL)
+                       return true;
+
                if (mpd_run_shuffle_range(connection, range.start, range.end))
-                       screen_status_message(_("Shuffled playlist"));
+                       screen_status_message(_("Shuffled queue"));
                else
                        mpdclient_handle_error(c);
                return true;
@@ -749,12 +747,16 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
                if (range.start == 0 || range.end <= range.start)
                        return false;
 
-               for (unsigned i = range.start; i < range.end; ++i)
-                       mpdclient_cmd_move(c, i, i - 1);
+               if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
+                       return true;
 
                lw->selected--;
                lw->range_base--;
 
+               if (lw->range_selection)
+                       list_window_scroll_to(lw, lw->range_base);
+               list_window_scroll_to(lw, lw->selected);
+
                screen_queue_save_selection();
                return true;
 
@@ -763,12 +765,16 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
                if (range.end >= playlist_length(&c->playlist))
                        return false;
 
-               for (int i = range.end - 1; i >= (int)range.start; --i)
-                       mpdclient_cmd_move(c, i, i + 1);
+               if (!mpdclient_cmd_move(c, range.start, range.end))
+                       return true;
 
                lw->selected++;
                lw->range_base++;
 
+               if (lw->range_selection)
+                       list_window_scroll_to(lw, lw->range_base);
+               list_window_scroll_to(lw, lw->selected);
+
                screen_queue_save_selection();
                return true;