From 50eb528bc1f9d2f7b7260eff8b85a5ed5b96e679 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Thu, 31 Jan 2013 07:24:19 +0100 Subject: scroll_engine: Major rework to support pixel-based scrolling and scroll callbacks. Much of the scrolling work is moved from lcd-bitmap-common to lcd-scroll.c, a small scroll callback routine remains. This callback can potentially be overridden by more extensive scrollers. The callback also gets fed with pixel-based scrolling information, which finally removes the strict line-based nature of the scroll engine. Along with this is the change from scroll_stop_viewport_line() to scroll_stop_viewport_rect() which works on a pixel-based rectangle instead of lines. The ultimate goal is to move most of the scroll work to apps, which can much better decide which line decorations to apply etc. This work is laying the ground work. Change-Id: I3b2885cf7d8696ddd9253d5a9a73318d3d42831a --- firmware/drivers/lcd-bitmap-common.c | 180 +++++++++++++---------------------- firmware/drivers/lcd-scroll.c | 111 ++++++++++++++++++--- 2 files changed, 161 insertions(+), 130 deletions(-) (limited to 'firmware/drivers') diff --git a/firmware/drivers/lcd-bitmap-common.c b/firmware/drivers/lcd-bitmap-common.c index c04f57ef22..f3e700a4a1 100644 --- a/firmware/drivers/lcd-bitmap-common.c +++ b/firmware/drivers/lcd-bitmap-common.c @@ -412,12 +412,10 @@ static void LCDFN(putsxyofs_style)(int xpos, int ypos, /*** Line oriented text output ***/ -/* put a string at a given char position */ void LCDFN(puts_style_xyoffset)(int x, int y, const unsigned char *str, int style, int x_offset, int y_offset) { int xpos, ypos, h; - LCDFN(scroll_stop_viewport_line)(current_vp, y); if(!str) return; @@ -425,14 +423,15 @@ void LCDFN(puts_style_xyoffset)(int x, int y, const unsigned char *str, if ((style&STYLE_XY_PIXELS) == 0) { xpos = x * LCDFN(getstringsize)(" ", NULL, NULL); - ypos = y * h + y_offset; + ypos = y * h; } else { xpos = x; - ypos = y + y_offset; + ypos = y; } - LCDFN(putsxyofs_style)(xpos, ypos, str, style, h, x_offset); + LCDFN(scroll_stop_viewport_rect)(current_vp, x, y, current_vp->width - x, h); + LCDFN(putsxyofs_style)(xpos, ypos+y_offset, str, style, h, x_offset); } void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str, @@ -469,7 +468,7 @@ void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset) /*** scrolling ***/ -static struct scrollinfo* find_scrolling_line(int line) +static struct scrollinfo* find_scrolling_line(int x, int y) { struct scrollinfo* s = NULL; int i; @@ -477,93 +476,101 @@ static struct scrollinfo* find_scrolling_line(int line) for(i=0; iy == line && s->vp == current_vp) + if (s->x == x && s->y == y && s->vp == current_vp) return s; } return NULL; } -void LCDFN(puts_scroll_style_xyoffset)(int x, int y, const unsigned char *string, - int style, int x_offset, int y_offset) +void LCDFN(scroll_fn)(struct scrollinfo* s) +{ + LCDFN(putsxyofs_style)(s->x, s->y, s->line, s->style, s->height, s->offset); +} + +static void LCDFN(puts_scroll_worker)(int x, int y, const unsigned char *string, + int style, int x_offset, int y_offset, + bool linebased, + void (*scroll_func)(struct scrollinfo *), + void *data) { struct scrollinfo* s; - char *end; - int w, h; - int len; - bool restart = false; - int space_width; + int width, height; + int w, h, cwidth, margin; + bool restart; - if (!string || ((unsigned)y >= (unsigned)current_vp->height)) + if (!string) return; - s = find_scrolling_line(y); - if (!s) - restart = true; + /* prepare rectangle for scrolling. x and y must be calculated early + * for find_scrolling_line() to work */ + cwidth = font_get(current_vp->font)->maxwidth; + height = current_vp->line_height ?: (int)font_get(current_vp->font)->height; + y = y * (linebased ? height : 1) + y_offset; + x = x * (linebased ? cwidth : 1); + width = current_vp->width - x; - if (restart) - { + if (y >= current_vp->height) + return; + + s = find_scrolling_line(x, y); + restart = !s; + + if (restart) { /* remove any previously scrolling line at the same location */ - LCDFN(scroll_stop_viewport_line)(current_vp, y); + LCDFN(scroll_stop_viewport_rect)(current_vp, x, y, width, height); + LCDFN(putsxyofs_style)(x, y, string, style, height, x_offset); - if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return; - LCDFN(puts_style_xyoffset)(x, y, string, style, x_offset, y_offset); + if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) + return; } + /* get width (pixeks) of the string */ LCDFN(getstringsize)(string, &w, &h); - if (current_vp->width - x * 8 >= w) + /* check if scrolling is actually necessary (consider the actual start + * of the line) */ + margin = x * linebased ? cwidth : 1; + if (current_vp->width >= margin+w) return; - if (restart) - { + if (restart) { /* prepare scroll line */ s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines]; s->start_tick = current_tick + LCDFN(scroll_info).delay; } - strlcpy(s->line, string, sizeof s->line); - space_width = LCDFN(getstringsize)(" ", NULL, NULL); - /* get width */ - LCDFN(getstringsize)(s->line, &w, &h); - if (!restart && s->width > w) - { - if (s->startx > w) - s->startx = w; - } - s->width = w; - - /* scroll bidirectional or forward only depending on the string - width */ + /* copy contents to the line buffer */ + strlcpy(s->linebuffer, string, sizeof(s->linebuffer)); + /* scroll bidirectional or forward only depending on the string width */ if ( LCDFN(scroll_info).bidir_limit ) { - s->bidir = s->width < (current_vp->width) * + s->bidir = w < (current_vp->width) * (100 + LCDFN(scroll_info).bidir_limit) / 100; } else s->bidir = false; - if (!s->bidir) { /* add spaces if scrolling in the round */ - strlcat(s->line, " ", sizeof s->line); - /* get new width incl. spaces */ - s->width += space_width * 3; - } - - end = strchr(s->line, '\0'); - len = sizeof s->line - (end - s->line); - strlcpy(end, string, MIN(current_vp->width/2, len)); + s->scroll_func = scroll_func; + s->userdata = data; - s->vp = current_vp; - s->y = y; - if (restart) - { + if (restart) { s->offset = x_offset; - s->startx = x * space_width; s->backward = false; s->style = style; + /* assign the rectangle. not necessary if continuing an earlier line */ + s->x = x; + s->y = y; + s->width = width; + s->height = height; + s->vp = current_vp; + LCDFN(scroll_info).lines++; } - s->y_offset = y_offset; +} - if (restart) - LCDFN(scroll_info).lines++; +void LCDFN(puts_scroll_style_xyoffset)(int x, int y, const unsigned char *string, + int style, int x_offset, int y_offset) +{ + LCDFN(puts_scroll_worker)(x, y, string, style, x_offset, y_offset, + true, LCDFN(scroll_fn), NULL); } void LCDFN(puts_scroll)(int x, int y, const unsigned char *string) @@ -583,65 +590,6 @@ void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string, LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset); } -void LCDFN(scroll_fn)(void) -{ - struct scrollinfo* s; - int index; - int xpos, ypos, height; - struct viewport* old_vp = current_vp; - bool makedelay; - - for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) { - s = &LCDFN(scroll_info).scroll[index]; - - /* check pause */ - if (TIME_BEFORE(current_tick, s->start_tick)) - continue; - - LCDFN(set_viewport)(s->vp); - height = s->vp->line_height ?: (int)font_get(s->vp->font)->height; - - if (s->backward) - s->offset -= LCDFN(scroll_info).step; - else - s->offset += LCDFN(scroll_info).step; - - xpos = s->startx; - ypos = s->y * height + s->y_offset; - - makedelay = false; - if (s->bidir) { /* scroll bidirectional */ - if (s->offset <= 0) { - /* at beginning of line */ - s->offset = 0; - s->backward = false; - makedelay = true; - } - else if (s->offset >= s->width - (current_vp->width - xpos)) { - /* at end of line */ - s->offset = s->width - (current_vp->width - xpos); - s->backward = true; - makedelay = true; - } - } - else { - /* scroll forward the whole time */ - if (s->offset >= s->width) { - s->offset = 0; - makedelay = true; - } - } - - if (makedelay) - s->start_tick = current_tick + LCDFN(scroll_info).delay + - LCDFN(scroll_info).ticks; - - LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, height, s->offset); - LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width-xpos, height); - } - LCDFN(set_viewport)(old_vp); -} - void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string, int style, int x_offset) { diff --git a/firmware/drivers/lcd-scroll.c b/firmware/drivers/lcd-scroll.c index ffd4663c79..31c2cf20b0 100644 --- a/firmware/drivers/lcd-scroll.c +++ b/firmware/drivers/lcd-scroll.c @@ -54,30 +54,27 @@ void LCDFN(scroll_stop)(void) LCDFN(scroll_info).lines = 0; } -/* Stop scrolling line y in the specified viewport, or all lines if y < 0 */ -void LCDFN(scroll_stop_viewport_line)(const struct viewport *current_vp, int line) +/* Clears scrolling lines that intersect with the area */ +void LCDFN(scroll_stop_viewport_rect)(const struct viewport *vp, int x, int y, int width, int height) { int i = 0; - while (i < LCDFN(scroll_info).lines) { - struct viewport *vp = LCDFN(scroll_info).scroll[i].vp; - if (((vp == current_vp)) && - ((line < 0) || (LCDFN(scroll_info).scroll[i].y == line))) + struct scrollinfo *s = &LCDFN(scroll_info).scroll[i]; + /* check if the specified area crosses the viewport in some way */ + if (s->vp == vp + && (x < (s->x+s->width) && (x+width) >= s->x) + && (y < (s->y+s->height) && (y+height) >= s->y)) { /* If i is not the last active line in the array, then move - the last item to position i */ + the last item to position i. This compacts + the scroll array at the same time of removing the line */ if ((i + 1) != LCDFN(scroll_info).lines) { LCDFN(scroll_info).scroll[i] = LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines-1]; } LCDFN(scroll_info).lines--; - - /* A line can only appear once, so we're done, - * unless we are clearing the whole viewport */ - if (line >= 0) - return ; } else { @@ -87,9 +84,9 @@ void LCDFN(scroll_stop_viewport_line)(const struct viewport *current_vp, int lin } /* Stop all scrolling lines in the specified viewport */ -void LCDFN(scroll_stop_viewport)(const struct viewport *current_vp) +void LCDFN(scroll_stop_viewport)(const struct viewport *vp) { - LCDFN(scroll_stop_viewport_line)(current_vp, -1); + LCDFN(scroll_stop_viewport_rect)(vp, 0, 0, vp->width, vp->height); } void LCDFN(scroll_speed)(int speed) @@ -125,3 +122,89 @@ void LCDFN(jump_scroll_delay)(int ms) LCDFN(scroll_info).jump_scroll_delay = ms / (HZ / 10); } #endif + +static void LCDFN(scroll_worker)(void) +{ + int index, width; + bool makedelay; + static char line_buf[SCROLL_LINE_SIZE]; + bool is_default; + struct scroll_screen_info *si = &LCDFN(scroll_info); + struct scrollinfo *s; + struct viewport *vp; + + unsigned fg_pattern, bg_pattern, drawmode; + + for ( index = 0; index < si->lines; index++ ) { + s = &si->scroll[index]; + + /* check pause */ + if (TIME_BEFORE(current_tick, s->start_tick)) + continue; + + s->start_tick = current_tick; + + /* this runs out of the ui thread, thus we need to + * save and restore the current viewport since the ui thread + * is unaware of the swapped viewports. the vp must + * be switched early so that lcd_getstringsize() picks the + * correct font */ + vp = LCDFN(get_viewport)(&is_default); + LCDFN(set_viewport)(s->vp); + + width = LCDFN(getstringsize)(s->linebuffer, NULL, NULL); + makedelay = false; + + if (s->backward) + s->offset -= si->step; + else + s->offset += si->step; + + if (s->bidir) { /* scroll bidirectional */ + + s->line = s->linebuffer; + if (s->offset <= 0) { + /* at beginning of line */ + s->offset = 0; + s->backward = false; + makedelay = true; + } + else if (s->offset >= width - (s->width - s->x)) { + /* at end of line */ + s->offset = width - (s->width - s->x); + s->backward = true; + makedelay = true; + } + } + else { + + snprintf(line_buf, sizeof(line_buf)-1, "%s%s%s", + s->linebuffer, " ", s->linebuffer); + s->line = line_buf; + width += LCDFN(getstringsize)(" ", NULL, NULL); + /* scroll forward the whole time */ + if (s->offset >= width) { + s->offset = 0; + makedelay = true; + } + } + + /* Stash and restore these three, so that the scroll_func + * can do whatever it likes without destroying the state */ + fg_pattern = s->vp->fg_pattern; + bg_pattern = s->vp->bg_pattern; + drawmode = s->vp->drawmode; + + s->scroll_func(s); + LCDFN(update_viewport_rect)(s->x, s->y, s->width, s->height); + + s->vp->fg_pattern = fg_pattern; + s->vp->bg_pattern = bg_pattern; + s->vp->drawmode = drawmode; + + LCDFN(set_viewport)(vp); + + if (makedelay) + s->start_tick += si->delay + si->ticks; + } +} -- cgit v1.2.3