Code

use libmpdclient2
[ncmpc.git] / src / screen_lyrics.c
index 1cb392fb86e4419f5e9de31f24a9f4e6b784e4ff..5714258dbc3d7f5d8958d2edd951188e9befda2c 100644 (file)
@@ -35,7 +35,8 @@
 
 static struct screen_text text;
 
-static const struct mpd_song *next_song;
+static struct mpd_song *next_song;
+static bool follow = false;
 
 static struct {
        struct mpd_song *song;
@@ -64,7 +65,7 @@ screen_lyrics_abort(void)
        }
 
        if (current.song != NULL) {
-               mpd_freeSong(current.song);
+               mpd_song_free(current.song);
                current.song = NULL;
        }
 }
@@ -82,6 +83,51 @@ lyrics_repaint_if_active(void)
        }
 }
 
+static bool
+exists_lyr_file(const char *artist, const char *title)
+{
+       char path[1024];
+       struct stat result;
+
+       snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
+                getenv("HOME"), artist, title);
+
+       return (stat(path, &result) == 0);
+}
+
+static FILE *
+create_lyr_file(const char *artist, const char *title)
+{
+       char path[1024];
+
+       snprintf(path, 1024, "%s/.lyrics",
+                getenv("HOME"));
+       mkdir(path, S_IRWXU);
+
+       snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
+                getenv("HOME"), artist, title);
+
+       return fopen(path, "w");
+}
+
+static int
+store_lyr_hd(void)
+{
+       FILE *lyr_file;
+       unsigned i;
+
+       lyr_file = create_lyr_file(current.artist, current.title);
+       if (lyr_file == NULL)
+               return -1;
+
+       for (i = 0; i < text.lines->len; ++i)
+               fprintf(lyr_file, "%s\n",
+                       (const char*)g_ptr_array_index(text.lines, i));
+
+       fclose(lyr_file);
+       return 0;
+}
+
 static void
 screen_lyrics_set(const GString *str)
 {
@@ -90,6 +136,10 @@ screen_lyrics_set(const GString *str)
        /* paint new data */
 
        lyrics_repaint_if_active();
+
+       if (options.lyrics_autosave &&
+           !exists_lyr_file(current.artist, current.title))
+               store_lyr_hd();
 }
 
 static void
@@ -117,7 +167,7 @@ screen_lyrics_load(const struct mpd_song *song)
        screen_lyrics_abort();
        screen_text_clear(&text);
 
-       current.song = mpd_songDup(song);
+       current.song = mpd_song_dup(song);
 
        strfsong(buffer, sizeof(buffer), "%artist%", song);
        current.artist = g_strdup(buffer);
@@ -129,37 +179,6 @@ screen_lyrics_load(const struct mpd_song *song)
                                     screen_lyrics_callback, NULL);
 }
 
-static FILE *create_lyr_file(const char *artist, const char *title)
-{
-       char path[1024];
-
-       snprintf(path, 1024, "%s/.lyrics",
-                getenv("HOME"));
-       mkdir(path, S_IRWXU);
-
-       snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
-                getenv("HOME"), artist, title);
-
-       return fopen(path, "w");
-}
-
-static int store_lyr_hd(void)
-{
-       FILE *lyr_file;
-       unsigned i;
-
-       lyr_file = create_lyr_file(current.artist, current.title);
-       if (lyr_file == NULL)
-               return -1;
-
-       for (i = 0; i < text.lines->len; ++i)
-               fprintf(lyr_file, "%s\n",
-                       (const char*)g_ptr_array_index(text.lines, i));
-
-       fclose(lyr_file);
-       return 0;
-}
-
 static void
 lyrics_screen_init(WINDOW *w, int cols, int rows)
 {
@@ -188,12 +207,31 @@ lyrics_open(mpdclient_t *c)
 
        if (next_song != NULL &&
            (current.song == NULL ||
-            strcmp(next_song->file, current.song->file) != 0))
+            strcmp(mpd_song_get_uri(next_song),
+                   mpd_song_get_uri(current.song)) != 0))
                screen_lyrics_load(next_song);
 
+       if (next_song != c->song)
+               mpd_song_free(next_song);
        next_song = NULL;
 }
 
+static void
+lyrics_update(mpdclient_t *c)
+{
+       if (!follow)
+               return;
+
+       next_song = c->song;
+
+       if (next_song != NULL &&
+           (current.song == NULL ||
+            strcmp(mpd_song_get_uri(next_song),
+                   mpd_song_get_uri(current.song)) != 0))
+               screen_lyrics_load(next_song);
+
+       next_song = NULL;
+}
 
 static const char *
 lyrics_title(char *str, size_t size)
@@ -256,6 +294,9 @@ lyrics_cmd(mpdclient_t *c, command_t cmd)
 
                break;
 #endif
+       case CMD_SCREEN_SWAP:
+               screen_swap(c, current.song);
+               return true;
 
        case CMD_LOCATE:
                if (current.song != NULL) {
@@ -276,6 +317,7 @@ const struct screen_functions screen_lyrics = {
        .init = lyrics_screen_init,
        .exit = lyrics_exit,
        .open = lyrics_open,
+       .update = lyrics_update,
        .close = NULL,
        .resize = lyrics_resize,
        .paint = lyrics_paint,
@@ -284,10 +326,11 @@ const struct screen_functions screen_lyrics = {
 };
 
 void
-screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
+screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
 {
        assert(song != NULL);
 
-       next_song = song;
+       follow = f;
+       next_song = mpd_song_dup(song);
        screen_switch(&screen_lyrics, c);
 }