Code

screen_song: make Path a proper label
[ncmpc.git] / src / screen_lyrics.c
index 3dcddbfe5691a5cc07d0cac11d4cfbacee615445..e655fc5f37c1fcb53e2ce79d62b9c4be3aa0bb67 100644 (file)
@@ -45,9 +45,11 @@ static bool follow = false;
 static struct {
        struct mpd_song *song;
 
-       char *artist, *title;
+       char *artist, *title, *plugin_name;
 
        struct plugin_cycle *loader;
+
+       guint loader_timeout;
 } current;
 
 static void
@@ -58,6 +60,16 @@ screen_lyrics_abort(void)
                current.loader = NULL;
        }
 
+       if (current.loader_timeout != 0) {
+               g_source_remove(current.loader_timeout);
+               current.loader_timeout = 0;
+       }
+
+       if (current.plugin_name != NULL) {
+               g_free(current.plugin_name);
+               current.plugin_name = NULL;
+       }
+
        if (current.artist != NULL) {
                g_free(current.artist);
                current.artist = NULL;
@@ -65,7 +77,7 @@ screen_lyrics_abort(void)
 
        if (current.title != NULL) {
                g_free(current.title);
-               current.artist = NULL;
+               current.title = NULL;
        }
 
        if (current.song != NULL) {
@@ -165,11 +177,12 @@ screen_lyrics_set(const GString *str)
 
 static void
 screen_lyrics_callback(const GString *result, const bool success,
-                      G_GNUC_UNUSED const char *plugin_name,
-                      G_GNUC_UNUSED void *data)
+                      const char *plugin_name, G_GNUC_UNUSED void *data)
 {
        assert(current.loader != NULL);
 
+       current.plugin_name = g_strdup(plugin_name);
+
        /* Display result, which may be lyrics or error messages */
        if (result != NULL)
                screen_lyrics_set(result);
@@ -183,8 +196,26 @@ screen_lyrics_callback(const GString *result, const bool success,
                screen_status_message (_("No lyrics"));
        }
 
+       if (current.loader_timeout != 0) {
+               g_source_remove(current.loader_timeout);
+               current.loader_timeout = 0;
+       }
+
+       plugin_stop(current.loader);
+       current.loader = NULL;
+}
+
+static gboolean
+screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
+{
        plugin_stop(current.loader);
        current.loader = NULL;
+
+       screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
+                            options.lyrics_timeout);
+
+       current.loader_timeout = 0;
+       return FALSE;
 }
 
 static void
@@ -206,6 +237,13 @@ screen_lyrics_load(const struct mpd_song *song)
 
        current.loader = lyrics_load(current.artist, current.title,
                                     screen_lyrics_callback, NULL);
+
+       if (options.lyrics_timeout != 0) {
+               current.loader_timeout =
+                       g_timeout_add_seconds(options.lyrics_timeout,
+                                             screen_lyrics_timeout_callback,
+                                             NULL);
+       }
 }
 
 static void
@@ -240,9 +278,10 @@ lyrics_open(struct mpdclient *c)
                    mpd_song_get_uri(current.song)) != 0))
                screen_lyrics_load(next_song_c);
 
-       if (next_song != NULL)
+       if (next_song != NULL) {
                mpd_song_free(next_song);
-       next_song = NULL;
+               next_song = NULL;
+       }
 }
 
 static void
@@ -270,9 +309,16 @@ lyrics_title(char *str, size_t size)
                return str;
        } else if (current.artist != NULL && current.title != NULL &&
                   !screen_text_is_empty(&text)) {
-               snprintf(str, size, "%s: %s - %s",
-                        _("Lyrics"),
-                        current.artist, current.title);
+               int n;
+               n = snprintf(str, size, "%s: %s - %s",
+                            _("Lyrics"),
+                            current.artist, current.title);
+
+               if (options.lyrics_show_plugin && current.plugin_name != NULL &&
+                   (unsigned int) n < size - 1)
+                       snprintf(str + n, size - n, " (%s)",
+                                current.plugin_name);
+
                return str;
        } else
                return _("Lyrics");