From: Max Kellermann Date: Sat, 3 Oct 2009 20:05:09 +0000 (+0200) Subject: screen_play, status_bar: update scrolling with a GLib timer X-Git-Tag: release-0.16~179 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ae8123c1752301ffbf45be66befa9d9166df3add;p=ncmpc.git screen_play, status_bar: update scrolling with a GLib timer --- diff --git a/src/hscroll.c b/src/hscroll.c index 784c17d..0ec5ad9 100644 --- a/src/hscroll.c +++ b/src/hscroll.c @@ -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; } diff --git a/src/hscroll.h b/src/hscroll.h index cad3f17..fadbae9 100644 --- a/src/hscroll.h +++ b/src/hscroll.h @@ -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); diff --git a/src/screen_play.c b/src/screen_play.c index 0abee2e..9560b2b 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -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 */ diff --git a/src/status_bar.c b/src/status_bar.c index 19eec1d..ddffb8e 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -31,6 +31,21 @@ #include #include +#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 */ diff --git a/src/status_bar.h b/src/status_bar.h index c97c7a1..1410aed 100644 --- a/src/status_bar.h +++ b/src/status_bar.h @@ -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 }