Code

disable more features with --enable-mini
[ncmpc.git] / src / screen_lyrics.c
index 50f41b7f6f4c83c0a27d72efbe5bb7d839b0e9dd..6d9c6e4af03ab96a700c0dc647f9d8ed0d114423 100644 (file)
  *
  */
 
-#include "config.h"
-#ifndef DISABLE_LYRICS_SCREEN
 #include <sys/stat.h>
-#include "ncmpc.h"
+#include "i18n.h"
 #include "options.h"
 #include "mpdclient.h"
 #include "command.h"
 #include "screen_utils.h"
 #include "strfsong.h"
 #include "lyrics.h"
+#include "charset.h"
 #include "gcc.h"
 
 #define _GNU_SOURCE
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include <ncurses.h>
 #include <unistd.h>
 #include <stdio.h>
 
 static list_window_t *lw = NULL;
 
+static const struct mpd_song *next_song;
+
 static struct {
        const struct mpd_song *song;
 
@@ -82,8 +82,31 @@ screen_lyrics_clear(void)
        g_ptr_array_set_size(current.lines, 0);
 }
 
-static const char *
-list_callback(unsigned idx, int *highlight, void *data);
+static void
+lyrics_paint(void);
+
+/**
+ * Repaint and update the screen.
+ */
+static void
+lyrics_repaint(void)
+{
+       lyrics_paint();
+       wrefresh(lw->w);
+}
+
+/**
+ * Repaint and update the screen, if it is currently active.
+ */
+static void
+lyrics_repaint_if_active(void)
+{
+       if (screen_is_visible(&screen_lyrics)) {
+               lyrics_repaint();
+
+               /* XXX repaint the screen title */
+       }
+}
 
 static void
 screen_lyrics_set(const GString *str)
@@ -125,12 +148,7 @@ screen_lyrics_set(const GString *str)
 
        /* paint new data */
 
-       if (get_cur_mode_id() == 104) { /* XXX don't use the literal number */
-               list_window_paint(lw, list_callback, NULL);
-               wrefresh(lw->w);
-
-               /* XXX repaint the screen title */
-       }
+       lyrics_repaint_if_active();
 }
 
 static void
@@ -148,7 +166,7 @@ screen_lyrics_callback(const GString *result, mpd_unused void *data)
 }
 
 static void
-screen_lyrics_load(struct mpd_song *song)
+screen_lyrics_load(const struct mpd_song *song)
 {
        char buffer[MAX_SONGNAME_LENGTH];
 
@@ -203,10 +221,17 @@ static int store_lyr_hd(void)
 static const char *
 list_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
 {
+       static char buffer[256];
+       char *value;
+
        if (idx >= current.lines->len)
                return NULL;
 
-       return g_ptr_array_index(current.lines, idx);
+       value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
+       g_strlcpy(buffer, value, sizeof(buffer));
+       free(value);
+
+       return buffer;
 }
 
 
@@ -238,10 +263,15 @@ lyrics_exit(void)
 }
 
 static void
-lyrics_open(mpd_unused screen_t *screen, mpdclient_t *c)
+lyrics_open(mpdclient_t *c)
 {
-       if (c->song != NULL && c->song != current.song)
-               screen_lyrics_load(c->song);
+       if (next_song == NULL)
+               next_song = c->song;
+
+       if (next_song != NULL && next_song != current.song)
+               screen_lyrics_load(next_song);
+
+       next_song = NULL;
 }
 
 
@@ -260,27 +290,18 @@ lyrics_title(char *str, size_t size)
 }
 
 static void
-lyrics_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
+lyrics_paint(void)
 {
        list_window_paint(lw, list_callback, NULL);
 }
 
-
-static void
-lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
-{
-       if( lw->repaint ) {
-               list_window_paint(lw, list_callback, NULL);
-               lw->repaint = 0;
-       }
-}
-
-
 static int
-lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
+lyrics_cmd(mpdclient_t *c, command_t cmd)
 {
-       if (list_window_scroll_cmd(lw, current.lines->len, cmd))
+       if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
+               lyrics_repaint();
                return 1;
+       }
 
        switch(cmd) {
        case CMD_INTERRUPT:
@@ -297,8 +318,7 @@ lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
        case CMD_LYRICS_UPDATE:
                if (c->song != NULL) {
                        screen_lyrics_load(c->song);
-                       lyrics_paint(NULL, NULL);
-                       wrefresh(lw->w);
+                       lyrics_repaint();
                }
                return 1;
        default:
@@ -306,11 +326,11 @@ lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
        }
 
        lw->selected = lw->start+lw->rows;
-       if (screen_find(screen,
-                       lw, current.lines->len,
+       if (screen_find(lw, current.lines->len,
                        cmd, list_callback, NULL)) {
                /* center the row */
                list_window_center(lw, current.lines->len, lw->selected);
+               lyrics_repaint();
                return 1;
        }
 
@@ -324,9 +344,15 @@ const struct screen_functions screen_lyrics = {
        .close = NULL,
        .resize = lyrics_resize,
        .paint = lyrics_paint,
-       .update = lyrics_update,
        .cmd = lyrics_cmd,
        .get_title = lyrics_title,
 };
 
-#endif /* ENABLE_LYRICS_SCREEN */
+void
+screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
+{
+       assert(song != NULL);
+
+       next_song = song;
+       screen_switch(&screen_lyrics, c);
+}