Code

screen_queue: always update the list size
[ncmpc.git] / src / screen_queue.c
index 491d3321070076d6d0827b90bdda861b04c0f33f..1ca3d62adabca9ca0feed0b6ee7a797147ee660d 100644 (file)
@@ -56,10 +56,7 @@ typedef struct
        struct mpdclient *c;
 } completion_callback_data_t;
 
-/*
 static struct hscroll hscroll;
-static guint scroll_source_id;
-*/
 #endif
 
 static struct mpdclient_playlist *playlist;
@@ -120,20 +117,6 @@ screen_queue_restore_selection(void)
        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, G_GNUC_UNUSED void *data)
 {
@@ -147,67 +130,24 @@ screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
 
        strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
 
-       /*
-#ifndef NCMPC_MINI
-       if (idx == lw->selected)
-       {
-               if (options.scroll && utf8_width(songname) > (unsigned)COLS) {
-                       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;
-                       }
-               }
-       }
-#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);
+       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) {
@@ -219,6 +159,32 @@ center_playing_item(struct mpdclient *c, bool center_cursor)
        list_window_fetch_cursor(lw);
 }
 
+G_GNUC_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,
@@ -410,8 +376,8 @@ static int
 handle_add_to_playlist(struct mpdclient *c)
 {
        gchar *path;
-#ifndef NCMPC_MINI
        GCompletion *gcmp;
+#ifndef NCMPC_MINI
        GList *list = NULL;
        GList *dir_list = NULL;
        completion_callback_data_t data;
@@ -425,18 +391,15 @@ handle_add_to_playlist(struct mpdclient *c)
        wrln_completion_callback_data = &data;
        wrln_pre_completion_callback = add_pre_completion_cb;
        wrln_post_completion_callback = add_post_completion_cb;
+#else
+       gcmp = NULL;
 #endif
 
        /* get path */
        path = screen_readln(_("Add"),
                             NULL,
                             NULL,
-#ifdef NCMPC_MINI
-                            NULL
-#else
-                            gcmp
-#endif
-                            );
+                            gcmp);
 
        /* destroy completion data */
 #ifndef NCMPC_MINI
@@ -463,6 +426,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
@@ -501,6 +469,7 @@ screen_queue_open(struct mpdclient *c)
        }
 
        screen_queue_restore_selection();
+       screen_queue_song_change(c->status);
 }
 
 static void
@@ -510,6 +479,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
@@ -541,54 +515,52 @@ screen_queue_paint_callback(WINDOW *w, unsigned i,
                            bool selected, G_GNUC_UNUSED void *data)
 {
        const struct mpd_song *song;
+       struct hscroll *row_hscroll;
 
        assert(playlist != NULL);
        assert(i < playlist_length(playlist));
 
        song = playlist_get(playlist, i);
 
+#ifdef NCMPC_MINI
+       row_hscroll = NULL;
+#else
+       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);
+                      song, row_hscroll);
 }
 
 static void
 screen_queue_paint(void)
 {
-       list_window_paint2(lw, screen_queue_paint_callback, NULL);
-}
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_clear(&hscroll);
+#endif
 
-G_GNUC_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;
+       list_window_paint2(lw, screen_queue_paint_callback, NULL);
 }
 
 static void
 screen_queue_update(struct mpdclient *c)
 {
-       if (c->events & MPD_IDLE_PLAYLIST)
+       if (c->events & MPD_IDLE_QUEUE)
                screen_queue_restore_selection();
+       else
+               /* the queue size may have changed, even if we havn't
+                  revceived the QUEUE idle event yet */
+               list_window_set_length(lw, playlist_length(playlist));
 
-       if ((c->events & MPD_IDLE_PLAYER) != 0 &&
-           get_current_song_id(c->status) != current_song_id) {
-               current_song_id = get_current_song_id(c->status);
-
-               /* 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 */
+       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_repaint();
-       }
 }
 
 #ifdef HAVE_GETMOUSE
@@ -616,8 +588,16 @@ handle_mouse_event(struct mpdclient *c)
 
        if (bstate & BUTTON1_CLICKED) {
                /* play */
-               if (lw->selected < 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 (lw->selected == old_selected)
@@ -640,6 +620,7 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
        static command_t cached_cmd = CMD_NONE;
        command_t prev_cmd = cached_cmd;
        struct list_window_range range;
+       const struct mpd_song *song;
 
        cached_cmd = cmd;
 
@@ -660,7 +641,7 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        switch(cmd) {
        case CMD_SCREEN_UPDATE:
-               center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
+               center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
                screen_queue_repaint();
                return false;
        case CMD_SELECT_PLAYING:
@@ -729,7 +710,15 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
 
        switch(cmd) {
        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:
@@ -768,8 +757,8 @@ 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--;
@@ -782,8 +771,8 @@ 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++;