Code

screen_play, status_bar: update scrolling with a GLib timer
authorMax Kellermann <max@duempel.org>
Sat, 3 Oct 2009 20:05:09 +0000 (22:05 +0200)
committerMax Kellermann <max@duempel.org>
Sat, 3 Oct 2009 20:05:09 +0000 (22:05 +0200)
src/hscroll.c
src/hscroll.h
src/screen_play.c
src/status_bar.c
src/status_bar.h

index 784c17d31c55794dc6d4499b20f850d2e2d33bf0..0ec5ad973f6adc03796a29709ae0bd10829f1325 100644 (file)
@@ -34,10 +34,8 @@ strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width)
        assert(str != NULL);
        assert(separator != NULL);
 
-       if (hscroll->offset == 0) {
-               ++hscroll->offset;
+       if (hscroll->offset == 0)
                return g_strdup(str);
-       }
 
        /* create a buffer containing the string and the separator */
        size = strlen(str)+strlen(separator)+1;
@@ -64,11 +62,6 @@ strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width)
                        g_strlcat(buf, tmp, size);
        }
 
-       if (time(NULL) - hscroll->t >= 1) {
-               hscroll->t = time(NULL);
-               hscroll->offset++;
-       }
-
        g_free(tmp);
        return buf;
 }
index cad3f17ddae93404c79774a9a4e4a76feafd607a..fadbae9af4ab978fe7b306a4a124499d98ebc164 100644 (file)
@@ -24,7 +24,6 @@
 
 struct hscroll {
        gsize offset;
-       GTime t; /* GTime is equivalent to time_t */
 };
 
 static inline void
@@ -33,6 +32,12 @@ hscroll_reset(struct hscroll *hscroll)
        hscroll->offset = 0;
 }
 
+static inline void
+hscroll_step(struct hscroll *hscroll)
+{
+       ++hscroll->offset;
+}
+
 char *
 strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width);
 
index 0abee2ee46f96b0be9c352bb43c9b2091e724f56..9560b2b9df4f48b68339d1cced3c22a63670bf4b 100644 (file)
@@ -56,10 +56,8 @@ typedef struct
        struct mpdclient *c;
 } completion_callback_data_t;
 
-#ifndef NCMPC_MINI
 static struct hscroll hscroll;
-#endif
-static bool must_scroll;
+static guint scroll_source_id;
 #endif
 
 static struct mpdclient_playlist *playlist;
@@ -119,6 +117,18 @@ playlist_restore_selection(void)
        playlist_save_selection();
 }
 
+#ifndef NCMPC_MINI
+static gboolean
+scroll_timer_callback(G_GNUC_UNUSED gpointer data)
+{
+       scroll_source_id = 0;
+
+       hscroll_step(&hscroll);
+       playlist_repaint();
+       return false;
+}
+#endif
+
 static const char *
 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
 {
@@ -152,8 +162,6 @@ 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) {
                                hscroll_reset(&hscroll);
                                current_song = lw->selected;
@@ -163,9 +171,20 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED
                                        MAX_SONG_LENGTH);
                        g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
                        g_free(tmp);
-               }
-               else
+
+                       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
        (void)second_column;
@@ -544,10 +563,6 @@ screen_playlist_title(char *str, size_t size)
 static void
 screen_playlist_paint(void)
 {
-#ifndef NCMPC_MINI
-       must_scroll = false;
-#endif
-
        list_window_paint(lw, list_callback, NULL);
 }
 
@@ -572,12 +587,6 @@ screen_playlist_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 */
index 19eec1d0c584b1eb52bacda87fc05990bfd9b443..ddffb8ee060f7c5ee577541ab89da0deebcc2953 100644 (file)
 #include <assert.h>
 #include <string.h>
 
+#ifndef NCMPC_MINI
+static gboolean
+scroll_timer_callback(gpointer data)
+{
+       struct status_bar *p = data;
+
+       p->scroll_source_id = 0;
+
+       hscroll_step(&p->hscroll);
+       status_bar_paint(p, p->prev_status, p->prev_song);
+       doupdate();
+       return false;
+}
+#endif
+
 static gboolean
 status_bar_clear_message(gpointer data)
 {
@@ -65,6 +80,11 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
        int x = 0;
        char buffer[p->window.cols * 4 + 1];
 
+#ifndef NCMPC_MINI
+       p->prev_status = status;
+       p->prev_song = song;
+#endif
+
        if (p->message_source_id != 0)
                return;
 
@@ -171,10 +191,24 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
 
                        g_strlcpy(songname, tmp, sizeof(songname));
                        g_free(tmp);
+
+                       if (p->scroll_source_id == 0)
+                               p->scroll_source_id =
+                                       g_timeout_add(1000,
+                                                     scroll_timer_callback,
+                                                     p);
+               } else if (p->scroll_source_id != 0) {
+                       g_source_remove(p->scroll_source_id);
+                       p->scroll_source_id = 0;
                }
 #endif
                //mvwaddnstr(w, 0, x, songname, width);
                mvwaddstr(w, 0, x, songname);
+#ifndef NCMPC_MINI
+       } else if (p->scroll_source_id != 0) {
+               g_source_remove(p->scroll_source_id);
+               p->scroll_source_id = 0;
+#endif
        }
 
        /* display time string */
index c97c7a1a6398a72b87d3bf9ef253f93d059f2699..1410aed989a8cd81b7fe8b5682c3108727066428 100644 (file)
@@ -40,6 +40,10 @@ struct status_bar {
 
 #ifndef NCMPC_MINI
        struct hscroll hscroll;
+       guint scroll_source_id;
+
+       const struct mpd_status *prev_status;
+       const struct mpd_song *prev_song;
 #endif
 };
 
@@ -55,7 +59,9 @@ status_bar_init(struct status_bar *p, unsigned width, int y, int x)
 
 #ifndef NCMPC_MINI
        hscroll_reset(&p->hscroll);
-       p->hscroll.t = 0;
+       p->scroll_source_id = 0;
+       p->prev_status = NULL;
+       p->prev_song = NULL;
 #endif
 }