Code

hscroll: changed hscroll_state_t typedef to a struct
authorMax Kellermann <max@duempel.org>
Sat, 3 Oct 2009 19:42:32 +0000 (21:42 +0200)
committerMax Kellermann <max@duempel.org>
Sat, 3 Oct 2009 19:42:32 +0000 (21:42 +0200)
src/hscroll.c
src/hscroll.h
src/screen_play.c
src/status_bar.c

index 065cb7054b0e573e35357368f4015ef67f4cf897..784c17d31c55794dc6d4499b20f850d2e2d33bf0 100644 (file)
 #include <ctype.h>
 #include <string.h>
 
-// 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;
 }
index 9c15a9d49dbfb415026000879b133d9f813c7de6..8b643830de5dca63fccc3f9622f6b7c0669d2295 100644 (file)
 
 #include <glib.h>
 
-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
index aaaace14505572e2a152414918985477bc91c66d..3690dd341e3c84dbc2dee34a2918398ad19b6e3b 100644 (file)
@@ -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;
index 04881afc939ca2ce2b61755b5b7735ed9afe0ac4..204a66ecb28166a7b6a7fadd211e11393fc1e123 100644 (file)
@@ -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);