Code

screen_play: repaint after the "select-playing" command
[ncmpc.git] / src / screen_play.c
index bd830bac23785ecf735d21c3bbd52da483f7c7c6..256ea36d2b9e5f03b5d593586cf4637c88e511a9 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#include "screen_file.h"
+#include "screen_play.h"
 #include "screen_interface.h"
+#include "screen_file.h"
+#include "screen_message.h"
+#include "screen_find.h"
 #include "config.h"
 #include "i18n.h"
 #include "charset.h"
 #include "utils.h"
 #include "strfsong.h"
 #include "wreadln.h"
-#include "command.h"
 #include "colors.h"
 #include "screen.h"
 #include "screen_utils.h"
-#include "screen_play.h"
+#include "screen_song.h"
+#include "screen_lyrics.h"
 
 #ifndef NCMPC_MINI
 #include "hscroll.h"
@@ -40,9 +43,7 @@
 #include <mpd/client.h>
 
 #include <ctype.h>
-#include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <glib.h>
 
 #define MAX_SONG_LENGTH 512
@@ -55,22 +56,23 @@ typedef struct
        struct mpdclient *c;
 } completion_callback_data_t;
 
-static bool must_scroll;
+static struct hscroll hscroll;
+static guint scroll_source_id;
 #endif
 
 static struct mpdclient_playlist *playlist;
 static int current_song_id = -1;
 static int selected_song_id = -1;
-static list_window_t *lw = NULL;
+static struct list_window *lw;
 static guint timer_hide_cursor_id;
 
 static void
-play_paint(void);
+screen_playlist_paint(void);
 
 static void
 playlist_repaint(void)
 {
-       play_paint();
+       screen_playlist_paint();
        wrefresh(lw->w);
 }
 
@@ -116,13 +118,14 @@ playlist_restore_selection(void)
 }
 
 #ifndef NCMPC_MINI
-static char *
-format_duration(unsigned duration)
+static gboolean
+scroll_timer_callback(G_GNUC_UNUSED gpointer data)
 {
-       if (duration == 0)
-               return NULL;
+       scroll_source_id = 0;
 
-       return g_strdup_printf("%d:%02d", duration / 60, duration % 60);
+       hscroll_step(&hscroll);
+       playlist_repaint();
+       return false;
 }
 #endif
 
@@ -130,9 +133,6 @@ static const char *
 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
 {
        static char songname[MAX_SONG_LENGTH];
-#ifndef NCMPC_MINI
-       static scroll_state_t st;
-#endif
        struct mpd_song *song;
 
        if (playlist == NULL || idx >= playlist_length(playlist))
@@ -145,8 +145,12 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED
        strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
 
 #ifndef NCMPC_MINI
-       if(second_column)
-               *second_column = format_duration(mpd_song_get_duration(song));
+       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)
        {
@@ -158,20 +162,29 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED
                        static unsigned current_song;
                        char *tmp;
 
-                       must_scroll = true;
-
                        if (current_song != lw->selected) {
-                               st.offset = 0;
+                               hscroll_reset(&hscroll);
                                current_song = lw->selected;
                        }
 
-                       tmp = strscroll(songname, options.scroll_sep,
-                                       MAX_SONG_LENGTH, &st);
+                       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
-                       st.offset = 0;
        }
 #else
        (void)second_column;
@@ -276,8 +289,8 @@ completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
 int
 playlist_save(struct mpdclient *c, char *name, char *defaultname)
 {
+       struct mpd_connection *connection;
        gchar *filename, *filename_utf8;
-       gint error;
 #ifndef NCMPC_MINI
        GCompletion *gcmp;
        GList *list = NULL;
@@ -325,12 +338,12 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname)
        /* send save command to mpd */
 
        filename_utf8 = locale_to_utf8(filename);
-       error = mpdclient_cmd_save_playlist(c, filename_utf8);
-
-       if (error) {
-               gint code = GET_ACK_ERROR_CODE(error);
 
-               if (code == MPD_SERVER_ERROR_EXIST) {
+       connection = mpdclient_get_connection(c);
+       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;
 
@@ -346,26 +359,23 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname)
                                return -1;
                        }
 
-                       error = mpdclient_cmd_delete_playlist(c, filename_utf8);
-                       if (error) {
+                       if (!mpd_run_rm(connection, filename_utf8) ||
+                           !mpd_run_save(connection, filename_utf8)) {
+                               mpdclient_handle_error(c);
                                g_free(filename_utf8);
                                g_free(filename);
                                return -1;
                        }
-
-                       error = mpdclient_cmd_save_playlist(c, filename_utf8);
-                       if (error) {
-                               g_free(filename_utf8);
-                               g_free(filename);
-                               return error;
-                       }
                } else {
+                       mpdclient_handle_error(c);
                        g_free(filename_utf8);
                        g_free(filename);
                        return -1;
                }
        }
 
+       c->events |= MPD_IDLE_STORED_PLAYLIST;
+
        g_free(filename_utf8);
 
        /* success */
@@ -476,7 +486,7 @@ handle_add_to_playlist(struct mpdclient *c)
 }
 
 static void
-play_init(WINDOW *w, int cols, int rows)
+screen_playlist_init(WINDOW *w, int cols, int rows)
 {
        lw = list_window_init(w, cols, rows);
 }
@@ -505,7 +515,7 @@ timer_hide_cursor(gpointer data)
 }
 
 static void
-play_open(struct mpdclient *c)
+screen_playlist_open(struct mpdclient *c)
 {
        playlist = &c->playlist;
 
@@ -520,7 +530,7 @@ play_open(struct mpdclient *c)
 }
 
 static void
-play_close(void)
+screen_playlist_close(void)
 {
        if (timer_hide_cursor_id != 0) {
                g_source_remove(timer_hide_cursor_id);
@@ -529,7 +539,7 @@ play_close(void)
 }
 
 static void
-play_resize(int cols, int rows)
+screen_playlist_resize(int cols, int rows)
 {
        lw->cols = cols;
        lw->rows = rows;
@@ -537,13 +547,13 @@ play_resize(int cols, int rows)
 
 
 static void
-play_exit(void)
+screen_playlist_exit(void)
 {
        list_window_free(lw);
 }
 
 static const char *
-play_title(char *str, size_t size)
+screen_playlist_title(char *str, size_t size)
 {
        if (options.host == NULL)
                return _("Playlist");
@@ -553,17 +563,13 @@ play_title(char *str, size_t size)
 }
 
 static void
-play_paint(void)
+screen_playlist_paint(void)
 {
-#ifndef NCMPC_MINI
-       must_scroll = false;
-#endif
-
        list_window_paint(lw, list_callback, NULL);
 }
 
 static void
-play_update(struct mpdclient *c)
+screen_playlist_update(struct mpdclient *c)
 {
        static int prev_song_id = -1;
 
@@ -571,7 +577,8 @@ play_update(struct mpdclient *c)
                playlist_restore_selection();
 
        current_song_id = c->status != NULL &&
-               !IS_STOPPED(mpd_status_get_state(c->status))
+               (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) {
@@ -582,12 +589,6 @@ play_update(struct mpdclient *c)
                        center_playing_item(c, false);
 
                playlist_repaint();
-#ifndef NCMPC_MINI
-       } else if (options.scroll && must_scroll) {
-               /* always repaint if horizontal scrolling is
-                  enabled */
-               playlist_repaint();
-#endif
        } else if (c->events & MPD_IDLE_PLAYLIST) {
                /* the playlist has changed, we must paint the new
                   version */
@@ -637,8 +638,9 @@ handle_mouse_event(struct mpdclient *c)
 #endif
 
 static bool
-play_cmd(struct mpdclient *c, command_t cmd)
+screen_playlist_cmd(struct mpdclient *c, command_t cmd)
 {
+       struct mpd_connection *connection;
        static command_t cached_cmd = CMD_NONE;
        command_t prev_cmd = cached_cmd;
        cached_cmd = cmd;
@@ -658,10 +660,81 @@ play_cmd(struct mpdclient *c, command_t cmd)
                return true;
        }
 
+       switch(cmd) {
+       case CMD_SCREEN_UPDATE:
+               center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
+               playlist_repaint();
+               return false;
+       case CMD_SELECT_PLAYING:
+               list_window_set_selected(lw, playlist_get_index(&c->playlist,
+                                                               c->song));
+               playlist_save_selection();
+               playlist_repaint();
+               return true;
+
+       case CMD_LIST_FIND:
+       case CMD_LIST_RFIND:
+       case CMD_LIST_FIND_NEXT:
+       case CMD_LIST_RFIND_NEXT:
+               screen_find(lw, playlist_length(&c->playlist),
+                           cmd, list_callback, NULL);
+               playlist_save_selection();
+               playlist_repaint();
+               return true;
+       case CMD_LIST_JUMP:
+               screen_jump(lw, list_callback, NULL);
+               playlist_save_selection();
+               playlist_repaint();
+               return true;
+
+#ifdef HAVE_GETMOUSE
+       case CMD_MOUSE_EVENT:
+               return handle_mouse_event(c);
+#endif
+
+#ifdef ENABLE_SONG_SCREEN
+       case CMD_SCREEN_SONG:
+               if (playlist_selected_song()) {
+                       screen_song_switch(c, playlist_selected_song());
+                       return true;
+               }
+
+               break;
+#endif
+
+#ifdef ENABLE_LYRICS_SCREEN
+       case CMD_SCREEN_LYRICS:
+               if (lw->selected < playlist_length(&c->playlist)) {
+                       struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
+                       bool follow = false;
+
+                       if (c->song && selected &&
+                           !strcmp(mpd_song_get_uri(selected),
+                                   mpd_song_get_uri(c->song)))
+                               follow = true;
+
+                       screen_lyrics_switch(c, selected, follow);
+                       return true;
+               }
+
+               break;
+#endif
+       case CMD_SCREEN_SWAP:
+               screen_swap(c, playlist_get(&c->playlist, lw->selected));
+               return true;
+
+       default:
+               break;
+       }
+
+       if (!mpdclient_is_connected(c))
+               return false;
+
        switch(cmd) {
        case CMD_PLAY:
                mpdclient_cmd_play(c, lw->selected);
                return true;
+
        case CMD_DELETE:
                if (lw->range_selection) {
                        mpdclient_cmd_delete_range(c, lw->selected_start,
@@ -677,29 +750,24 @@ play_cmd(struct mpdclient *c, command_t cmd)
        case CMD_SAVE_PLAYLIST:
                playlist_save(c, NULL, NULL);
                return true;
+
        case CMD_ADD:
                handle_add_to_playlist(c);
                return true;
-       case CMD_SCREEN_UPDATE:
-               center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
-               playlist_repaint();
-               return false;
-       case CMD_SELECT_PLAYING:
-               list_window_set_selected(lw, playlist_get_index(&c->playlist,
-                                                               c->song));
-               playlist_save_selection();
-               return true;
+
        case CMD_SHUFFLE:
-       {
                if(!lw->range_selection)
                        /* No range selection, shuffle all list. */
                        break;
 
-               if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
+               connection = mpdclient_get_connection(c);
+               if (mpd_run_shuffle_range(connection, lw->selected_start,
+                                         lw->selected_end + 1))
                        screen_status_message(_("Shuffled playlist"));
-
+               else
+                       mpdclient_handle_error(c);
                return true;
-       }
+
        case CMD_LIST_MOVE_UP:
                if(lw->selected_start == 0)
                        return false;
@@ -724,6 +792,7 @@ play_cmd(struct mpdclient *c, command_t cmd)
 
                playlist_save_selection();
                return true;
+
        case CMD_LIST_MOVE_DOWN:
                if(lw->selected_end+1 >= playlist_length(&c->playlist))
                        return false;
@@ -748,35 +817,6 @@ play_cmd(struct mpdclient *c, command_t cmd)
 
                playlist_save_selection();
                return true;
-       case CMD_LIST_FIND:
-       case CMD_LIST_RFIND:
-       case CMD_LIST_FIND_NEXT:
-       case CMD_LIST_RFIND_NEXT:
-               screen_find(lw, playlist_length(&c->playlist),
-                           cmd, list_callback, NULL);
-               playlist_save_selection();
-               playlist_repaint();
-               return true;
-       case CMD_LIST_JUMP:
-               screen_jump(lw, list_callback, NULL);
-               playlist_save_selection();
-               playlist_repaint();
-               return true;
-
-#ifdef HAVE_GETMOUSE
-       case CMD_MOUSE_EVENT:
-               return handle_mouse_event(c);
-#endif
-
-#ifdef ENABLE_SONG_SCREEN
-       case CMD_SCREEN_SONG:
-               if (playlist_selected_song()) {
-                       screen_song_switch(c, playlist_selected_song());
-                       return true;
-               }
-
-               break;
-#endif
 
        case CMD_LOCATE:
                if (playlist_selected_song()) {
@@ -786,27 +826,6 @@ play_cmd(struct mpdclient *c, command_t cmd)
 
                break;
 
-#ifdef ENABLE_LYRICS_SCREEN
-       case CMD_SCREEN_LYRICS:
-               if (lw->selected < playlist_length(&c->playlist)) {
-                       struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
-                       bool follow = false;
-
-                       if (c->song && selected &&
-                           !strcmp(mpd_song_get_uri(selected),
-                                   mpd_song_get_uri(c->song)))
-                               follow = true;
-
-                       screen_lyrics_switch(c, selected, follow);
-                       return true;
-               }
-
-               break;
-#endif
-       case CMD_SCREEN_SWAP:
-               screen_swap(c, playlist_get(&c->playlist, lw->selected));
-               return true;
-
        default:
                break;
        }
@@ -815,13 +834,13 @@ play_cmd(struct mpdclient *c, command_t cmd)
 }
 
 const struct screen_functions screen_playlist = {
-       .init = play_init,
-       .exit = play_exit,
-       .open = play_open,
-       .close = play_close,
-       .resize = play_resize,
-       .paint = play_paint,
-       .update = play_update,
-       .cmd = play_cmd,
-       .get_title = play_title,
+       .init = screen_playlist_init,
+       .exit = screen_playlist_exit,
+       .open = screen_playlist_open,
+       .close = screen_playlist_close,
+       .resize = screen_playlist_resize,
+       .paint = screen_playlist_paint,
+       .update = screen_playlist_update,
+       .cmd = screen_playlist_cmd,
+       .get_title = screen_playlist_title,
 };