Code

screen: moved screen_interface externs to each screen header
[ncmpc.git] / src / screen_play.c
index 8bbe60b8b691ffe5b511eba580ee7577388168f3..ceb13616662197b076a0316e9fdfbb3dc735a971 100644 (file)
@@ -17,6 +17,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include "screen_play.h"
+#include "screen_interface.h"
+#include "screen_file.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"
@@ -38,9 +41,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
@@ -57,8 +58,8 @@ static bool must_scroll;
 #endif
 
 static struct mpdclient_playlist *playlist;
-static unsigned visible_version = -1;
 static int current_song_id = -1;
+static int selected_song_id = -1;
 static list_window_t *lw = NULL;
 static guint timer_hide_cursor_id;
 
@@ -82,23 +83,35 @@ playlist_selected_song(void)
 }
 
 static void
-playlist_changed_callback(struct mpdclient *c, int event, gpointer data)
+playlist_save_selection(void)
 {
-       switch (event) {
-       case PLAYLIST_EVENT_DELETE:
-               break;
-       case PLAYLIST_EVENT_MOVE:
-               if (!lw->range_selection) {
-                       lw->selected = *((int *) data);
-                       if (lw->selected < lw->start)
-                               lw->start--;
-               }
-               break;
-       default:
-               break;
-       }
+       selected_song_id = playlist_selected_song() != NULL
+               ? (int)mpd_song_get_id(playlist_selected_song())
+               : -1;
+}
+
+static void
+playlist_restore_selection(void)
+{
+       const struct mpd_song *song;
+       int pos;
+
+       if (selected_song_id < 0)
+               /* there was no selection */
+               return;
+
+       song = playlist_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);
+       if (pos >= 0)
+               lw->selected = pos;
 
-       list_window_check_selected(lw, c->playlist.list->len);
+       list_window_check_selected(lw, playlist_length(playlist));
+       playlist_save_selection();
 }
 
 #ifndef NCMPC_MINI
@@ -169,11 +182,12 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED
 static void
 center_playing_item(struct mpdclient *c, bool center_cursor)
 {
+       const struct mpd_song *song;
        unsigned length = c->playlist.list->len;
        int idx;
 
-       if (!c->song || c->status == NULL ||
-           IS_STOPPED(mpd_status_get_state(c->status)))
+       song = mpdclient_get_current_song(c);
+       if (song == NULL)
                return;
 
        /* try to center the song that are playing */
@@ -492,8 +506,6 @@ timer_hide_cursor(gpointer data)
 static void
 play_open(struct mpdclient *c)
 {
-       static gboolean install_cb = TRUE;
-
        playlist = &c->playlist;
 
        assert(timer_hide_cursor_id == 0);
@@ -503,10 +515,7 @@ play_open(struct mpdclient *c)
                                                     timer_hide_cursor, c);
        }
 
-       if (install_cb) {
-               mpdclient_install_playlist_callback(c, playlist_changed_callback);
-               install_cb = FALSE;
-       }
+       playlist_restore_selection();
 }
 
 static void
@@ -550,8 +559,6 @@ play_paint(void)
 #endif
 
        list_window_paint(lw, list_callback, NULL);
-
-       visible_version = playlist->id;
 }
 
 static void
@@ -559,9 +566,12 @@ play_update(struct mpdclient *c)
 {
        static int prev_song_id = -1;
 
-       current_song_id = c->song != NULL && c->status != NULL &&
+       if (c->events & MPD_IDLE_PLAYLIST)
+               playlist_restore_selection();
+
+       current_song_id = c->status != NULL &&
                !IS_STOPPED(mpd_status_get_state(c->status))
-               ? (int)mpd_song_get_id(c->song) : -1;
+               ? (int)mpd_status_get_song_id(c->status) : -1;
 
        if (current_song_id != prev_song_id) {
                prev_song_id = current_song_id;
@@ -577,7 +587,7 @@ play_update(struct mpdclient *c)
                   enabled */
                playlist_repaint();
 #endif
-       } else if (visible_version != playlist->id) {
+       } else if (c->events & MPD_IDLE_PLAYLIST) {
                /* the playlist has changed, we must paint the new
                   version */
                playlist_repaint();
@@ -618,6 +628,7 @@ handle_mouse_event(struct mpdclient *c)
 
        lw->selected = selected;
        list_window_check_selected(lw, playlist_length(playlist));
+       playlist_save_selection();
        playlist_repaint();
 
        return true;
@@ -641,6 +652,7 @@ play_cmd(struct mpdclient *c, command_t cmd)
        }
 
        if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
+               playlist_save_selection();
                playlist_repaint();
                return true;
        }
@@ -650,21 +662,17 @@ play_cmd(struct mpdclient *c, command_t cmd)
                mpdclient_cmd_play(c, lw->selected);
                return true;
        case CMD_DELETE:
-       {
-               int i = lw->selected_end, start = lw->selected_start;
-               for(; i >= start; --i)
-                       mpdclient_cmd_delete(c, i);
-
-               i++;
-               if(i >= (int)playlist_length(&c->playlist))
-                       i--;
-               lw->selected = i;
-               lw->selected_start = i;
-               lw->selected_end = i;
-               lw->range_selection = false;
+               if (lw->range_selection) {
+                       mpdclient_cmd_delete_range(c, lw->selected_start,
+                                                  lw->selected_end + 1);
+               } else {
+                       mpdclient_cmd_delete(c, lw->selected);
+               }
 
+               lw->selected = lw->selected_end = lw->selected_start;
+               lw->range_selection = false;
                return true;
-       }
+
        case CMD_SAVE_PLAYLIST:
                playlist_save(c, NULL, NULL);
                return true;
@@ -678,6 +686,7 @@ play_cmd(struct mpdclient *c, command_t cmd)
        case CMD_SELECT_PLAYING:
                list_window_set_selected(lw, playlist_get_index(&c->playlist,
                                                                c->song));
+               playlist_save_selection();
                return true;
        case CMD_SHUFFLE:
        {
@@ -711,6 +720,8 @@ play_cmd(struct mpdclient *c, command_t cmd)
                        lw->selected_start--;
                        lw->selected_end--;
                }
+
+               playlist_save_selection();
                return true;
        case CMD_LIST_MOVE_DOWN:
                if(lw->selected_end+1 >= playlist_length(&c->playlist))
@@ -733,6 +744,8 @@ play_cmd(struct mpdclient *c, command_t cmd)
                        lw->selected_start++;
                        lw->selected_end++;
                }
+
+               playlist_save_selection();
                return true;
        case CMD_LIST_FIND:
        case CMD_LIST_RFIND:
@@ -740,10 +753,12 @@ play_cmd(struct mpdclient *c, command_t cmd)
        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;