From: Max Kellermann Date: Sat, 3 Oct 2009 19:42:32 +0000 (+0200) Subject: hscroll: changed hscroll_state_t typedef to a struct X-Git-Tag: release-0.16~184 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=64e5b56efe18bea2e2b22d0cc81d499868f9823e;p=ncmpc.git hscroll: changed hscroll_state_t typedef to a struct --- diff --git a/src/hscroll.c b/src/hscroll.c index 065cb70..784c17d 100644 --- a/src/hscroll.c +++ b/src/hscroll.c @@ -24,19 +24,18 @@ #include #include -// FIXME: utf-8 length char * -strscroll(char *str, char *separator, int width, scroll_state_t *st) +strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width) { gchar *tmp, *buf; gsize len, size; + assert(hscroll != NULL); assert(str != NULL); assert(separator != NULL); - assert(st != NULL); - if( st->offset==0 ) { - st->offset++; + if (hscroll->offset == 0) { + ++hscroll->offset; return g_strdup(str); } @@ -47,27 +46,29 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st) g_strlcat(tmp, separator, size); len = utf8_width(tmp); - if (st->offset >= len) - st->offset = 0; + if (hscroll->offset >= len) + hscroll->offset = 0; /* create the new scrolled string */ size = width+1; if (g_utf8_validate(tmp, -1, NULL) ) { - int ulen; + size_t ulen; buf = g_malloc(size*6);// max length of utf8 char is 6 - g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size); + g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp, hscroll->offset), size); if( (ulen = g_utf8_strlen(buf, -1)) < width ) g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1); } else { buf = g_malloc(size); - g_strlcpy(buf, tmp+st->offset, size); + g_strlcpy(buf, tmp + hscroll->offset, size); if (strlen(buf) < (size_t)width) g_strlcat(buf, tmp, size); } - if( time(NULL)-st->t >= 1 ) { - st->t = time(NULL); - st->offset++; + + 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 9c15a9d..8b64383 100644 --- a/src/hscroll.h +++ b/src/hscroll.h @@ -22,11 +22,12 @@ #include -typedef struct { +struct hscroll { gsize offset; GTime t; /* GTime is equivalent to time_t */ -} scroll_state_t; +}; -char *strscroll(char *str, char *separator, int width, scroll_state_t *st); +char * +strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width); #endif diff --git a/src/screen_play.c b/src/screen_play.c index aaaace1..3690dd3 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -121,7 +121,7 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED { static char songname[MAX_SONG_LENGTH]; #ifndef NCMPC_MINI - static scroll_state_t st; + static struct hscroll hscroll; #endif struct mpd_song *song; @@ -155,17 +155,17 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED must_scroll = true; if (current_song != lw->selected) { - st.offset = 0; + hscroll.offset = 0; current_song = lw->selected; } - tmp = strscroll(songname, options.scroll_sep, - MAX_SONG_LENGTH, &st); + tmp = strscroll(&hscroll, songname, options.scroll_sep, + MAX_SONG_LENGTH); g_strlcpy(songname, tmp, MAX_SONG_LENGTH); g_free(tmp); } else - st.offset = 0; + hscroll.offset = 0; } #else (void)second_column; diff --git a/src/status_bar.c b/src/status_bar.c index 04881af..204a66e 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -170,8 +170,9 @@ status_bar_paint(const struct status_bar *p, const struct mpd_status *status, /* scroll if the song name is to long */ #ifndef NCMPC_MINI if (options.scroll && utf8_width(songname) > (unsigned)width) { - static scroll_state_t st = { 0, 0 }; - char *tmp = strscroll(songname, options.scroll_sep, width, &st); + static struct hscroll hscroll = { 0, 0 }; + char *tmp = strscroll(&hscroll, songname, + options.scroll_sep, width); g_strlcpy(songname, tmp, sizeof(songname)); g_free(tmp);