mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 10/20] fbconsole: collect renderable state into struct fbc_screen_state
Date: Sun,  3 May 2026 10:33:12 +0200	[thread overview]
Message-ID: <20260503084430.2765761-11-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260503084430.2765761-1-a.fatoum@barebox.org>

Group the cursor position, color and attribute flag state that together
describe what the terminal is currently rendering into a single
struct fbc_screen_state.

This will allow us later on to implement DEC save/restore (\e7/\e8)
as single struct assignment.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/video/fbconsole.c | 129 ++++++++++++++++++++------------------
 1 file changed, 69 insertions(+), 60 deletions(-)

diff --git a/drivers/video/fbconsole.c b/drivers/video/fbconsole.c
index 74b37b429f2a..1c4418757b43 100644
--- a/drivers/video/fbconsole.c
+++ b/drivers/video/fbconsole.c
@@ -41,6 +41,22 @@ enum ansi_color {
 #define DEFAULT_COLOR	WHITE
 #define DEFAULT_BGCOLOR	BLACK
 
+struct fbc_screen_state {
+	unsigned int x, y; /* cursor position */
+
+	int color;
+	int bgcolor;
+
+	u32 color_rgb;
+	u32 bgcolor_rgb;
+
+#define ANSI_FLAG_INVERT	(1 << 0)
+#define ANSI_FLAG_BRIGHT	(1 << 1)
+#define SGR_ATTRIBUTES		(ANSI_FLAG_INVERT | ANSI_FLAG_BRIGHT)
+#define HIDE_CURSOR		(1 << 2)
+	unsigned flags;
+};
+
 struct fbc_priv {
 	struct console_device cdev;
 	struct fb_info *fb;
@@ -59,23 +75,12 @@ struct fbc_priv {
 	const struct font_desc *font;
 
 	unsigned int cols, rows;
-	unsigned int x, y; /* cursor position */
+
+	struct fbc_screen_state cur;
 
 	unsigned int rotation;
 	enum state_t state;
 
-	int color;
-	int bgcolor;
-
-	u32 color_rgb;
-	u32 bgcolor_rgb;
-
-#define ANSI_FLAG_INVERT	(1 << 0)
-#define ANSI_FLAG_BRIGHT	(1 << 1)
-#define SGR_ATTRIBUTES		(ANSI_FLAG_INVERT | ANSI_FLAG_BRIGHT)
-#define HIDE_CURSOR		(1 << 2)
-	unsigned flags;
-
 	int csipos;
 	u8 csi[256];
 	unsigned char csi_cmd;
@@ -262,10 +267,10 @@ static void drawchar(struct fbc_priv *priv, int x, int y, int c)
 
 			if (*inbuf & mask)
 				gu_set_pixel(priv->fb, adr + j * xstep,
-					     priv->color_rgb);
+					     priv->cur.color_rgb);
 			else
 				gu_set_pixel(priv->fb, adr + j * xstep,
-					     priv->bgcolor_rgb);
+					     priv->cur.bgcolor_rgb);
 
 			mask >>= 1;
 
@@ -327,8 +332,8 @@ static void video_invertchar(struct fbc_priv *priv, int x, int y)
 
 static void toggle_cursor_visibility(struct fbc_priv *priv)
 {
-	if (!(priv->flags & HIDE_CURSOR))
-		video_invertchar(priv, priv->x, priv->y);
+	if (!(priv->cur.flags & HIDE_CURSOR))
+		video_invertchar(priv, priv->cur.x, priv->cur.y);
 }
 
 static void fb_scroll_up_0(struct fbc_priv *priv, void *adr, int width, int height)
@@ -445,45 +450,47 @@ static void fb_scroll_up(struct fbc_priv *priv)
 
 static void printchar(struct fbc_priv *priv, int c)
 {
+	struct fbc_screen_state *cur = &priv->cur;
+
 	toggle_cursor_visibility(priv);
 
 	switch (c) {
 	case '\007': /* bell: ignore */
 		break;
 	case '\b':
-		if (priv->x > 0) {
-			priv->x--;
-		} else if (priv->y > 0) {
-			priv->x = priv->cols - 1;
-			priv->y--;
+		if (cur->x > 0) {
+			cur->x--;
+		} else if (cur->y > 0) {
+			cur->x = priv->cols - 1;
+			cur->y--;
 		}
 		break;
 	case '\n':
 	case '\013': /* Vertical tab is the same as Line Feed */
-		priv->y++;
+		cur->y++;
 		break;
 
 	case '\r':
-		priv->x = 0;
+		cur->x = 0;
 		break;
 
 	case '\t':
-		priv->x = (priv->x + 8) & ~0x3;
+		cur->x = (cur->x + 8) & ~0x3;
 		break;
 
 	default:
-		drawchar(priv, priv->x, priv->y, c);
+		drawchar(priv, priv->cur.x, priv->cur.y, c);
 
-		priv->x++;
-		if (priv->x >= priv->cols) {
-			priv->y++;
-			priv->x = 0;
+		cur->x++;
+		if (cur->x >= priv->cols) {
+			cur->y++;
+			cur->x = 0;
 		}
 	}
 
-	if (priv->y >= priv->rows) {
+	if (cur->y >= priv->rows) {
 		fb_scroll_up(priv);
-		priv->y = priv->rows - 1;
+		cur->y = priv->rows - 1;
 	}
 
 	toggle_cursor_visibility(priv);
@@ -491,29 +498,30 @@ static void printchar(struct fbc_priv *priv, int c)
 
 static void fbc_update_colors(struct fbc_priv *priv, int color, int bgcolor)
 {
+	struct fbc_screen_state *cur = &priv->cur;
 	struct rgb *rgb;
 
 	if (color >= 0)
-		priv->color = color;
+		cur->color = color;
 	if (bgcolor >= 0)
-		priv->bgcolor = bgcolor;
+		cur->bgcolor = bgcolor;
 
-	if (priv->flags & ANSI_FLAG_INVERT) {
-		color = priv->bgcolor;
-		bgcolor = priv->color;
+	if (cur->flags & ANSI_FLAG_INVERT) {
+		color = cur->bgcolor;
+		bgcolor = cur->color;
 	} else {
-		color = priv->color;
-		bgcolor = priv->bgcolor;
+		color = cur->color;
+		bgcolor = cur->bgcolor;
 	}
 
-	if (priv->flags & ANSI_FLAG_BRIGHT)
+	if (cur->flags & ANSI_FLAG_BRIGHT)
 		color += BRIGHT;
 
 	rgb = &colors[color];
-	priv->color_rgb = gu_rgb_to_pixel(priv->fb, rgb->r, rgb->g, rgb->b, 0xff);
+	cur->color_rgb = gu_rgb_to_pixel(priv->fb, rgb->r, rgb->g, rgb->b, 0xff);
 
 	rgb = &colors[bgcolor];
-	priv->bgcolor_rgb = gu_rgb_to_pixel(priv->fb, rgb->r, rgb->g, rgb->b, 0x0);
+	cur->bgcolor_rgb = gu_rgb_to_pixel(priv->fb, rgb->r, rgb->g, rgb->b, 0x0);
 }
 
 static void fbc_reset_colors(struct fbc_priv *priv)
@@ -523,6 +531,7 @@ static void fbc_reset_colors(struct fbc_priv *priv)
 
 static void fbc_parse_colors(struct fbc_priv *priv)
 {
+	struct fbc_screen_state *cur = &priv->cur;
 	int color = -1, bgcolor = -1;
 	int code;
 	char *str;
@@ -533,15 +542,15 @@ static void fbc_parse_colors(struct fbc_priv *priv)
 		code = simple_strtoul(str, &str, 10);
 		switch (code) {
 		case 0:
-			priv->flags &= ~SGR_ATTRIBUTES;
+			cur->flags &= ~SGR_ATTRIBUTES;
 			color = DEFAULT_COLOR;
 			bgcolor = DEFAULT_BGCOLOR;
 			break;
 		case 1:
-			priv->flags |= ANSI_FLAG_BRIGHT;
+			cur->flags |= ANSI_FLAG_BRIGHT;
 			break;
 		case 7:
-			priv->flags |= ANSI_FLAG_INVERT;
+			cur->flags |= ANSI_FLAG_INVERT;
 			break;
 		case 30 ... 37:
 			color = code - 30;
@@ -556,7 +565,7 @@ static void fbc_parse_colors(struct fbc_priv *priv)
 			bgcolor = DEFAULT_BGCOLOR;
 			break;
 		case 90 ... 97:
-			priv->flags |= ANSI_FLAG_BRIGHT;
+			cur->flags |= ANSI_FLAG_BRIGHT;
 			color = code - 90;
 			break;
 		}
@@ -571,12 +580,12 @@ static void fbc_parse_colors(struct fbc_priv *priv)
 
 static void fbc_set_cursor_row(struct fbc_priv *priv, int y)
 {
-	priv->y = clamp_t(int, y, 0, priv->rows - 1);
+	priv->cur.y = clamp_t(int, y, 0, priv->rows - 1);
 }
 
 static void fbc_set_cursor_col(struct fbc_priv *priv, unsigned int x)
 {
-	priv->x = clamp_t(int, x, 0, priv->cols - 1);
+	priv->cur.x = clamp_t(int, x, 0, priv->cols - 1);
 }
 
 static bool fbc_parse_csi(struct fbc_priv *priv)
@@ -595,10 +604,10 @@ static bool fbc_parse_csi(struct fbc_priv *priv)
 		/* suffix for vt100 "[?25h" */
 		switch (priv->csi_cmd) {
 		case '?': /* cursor visible */
-			if (!(priv->flags & HIDE_CURSOR))
+			if (!(priv->cur.flags & HIDE_CURSOR))
 				break;
 
-			priv->flags &= ~HIDE_CURSOR;
+			priv->cur.flags &= ~HIDE_CURSOR;
 			/* show cursor now */
 			toggle_cursor_visibility(priv);
 			return true;
@@ -610,7 +619,7 @@ static bool fbc_parse_csi(struct fbc_priv *priv)
 		case '?': /* cursor invisible */
 			/* hide cursor now */
 			toggle_cursor_visibility(priv);
-			priv->flags |= HIDE_CURSOR;
+			priv->cur.flags |= HIDE_CURSOR;
 			return true;
 		}
 		break;
@@ -634,12 +643,12 @@ static bool fbc_parse_csi(struct fbc_priv *priv)
 		toggle_cursor_visibility(priv);
 		switch (pos) {
 		case 0:
-			for (i = priv->x; i < priv->cols; i++)
-				drawchar(priv, i, priv->y, ' ');
+			for (i = priv->cur.x; i < priv->cols; i++)
+				drawchar(priv, i, priv->cur.y, ' ');
 			break;
 		case 1:
-			for (i = 0; i <= priv->x; i++)
-				drawchar(priv, i, priv->y, ' ');
+			for (i = 0; i <= priv->cur.x; i++)
+				drawchar(priv, i, priv->cur.y, ' ');
 			break;
 		}
 		toggle_cursor_visibility(priv);
@@ -771,7 +780,7 @@ static int setup_font(struct fbc_priv *priv)
 	if (priv->rows != newrows || priv->cols != newcols) {
 		priv->rows = newrows;
 		priv->cols = newcols;
-		priv->x = priv->y = 0;
+		priv->cur.x = priv->cur.y = 0;
 	}
 
 	return 0;
@@ -866,8 +875,8 @@ static int set_rotation(struct param_d *p, void *vpriv)
 	struct fbc_priv *priv = vpriv;
 
 	cls(priv);
-	priv->x = 0;
-	priv->y = 0;
+	priv->cur.x = 0;
+	priv->cur.y = 0;
 	setup_font(priv);
 
 	return 0;
@@ -897,8 +906,8 @@ int register_fbconsole(struct fb_info *fb)
 		fbname += 2;
 
 	priv->fb = fb;
-	priv->x = 0;
-	priv->y = 0;
+	priv->cur.x = 0;
+	priv->cur.y = 0;
 
 	fbc_reset_colors(priv);
 
-- 
2.47.3




  parent reply	other threads:[~2026-05-03  8:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-03  8:33 [PATCH 00/20] fbconsole: support TUI-relevant escape sequences Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 01/20] fbconsole: remove incomplete CSI_CNT state Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 02/20] fbconsole: improve handling of unexpected escape sequences Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 03/20] fbconsole: fix handling of CSI buffer overflow Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 04/20] fbconsole: do not reset cursor visibility alongside attributes Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 05/20] fbconsole: respect hidden cursor everywhere Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 06/20] fbconsole: call fb_blit_area for every drawchar Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 07/20] fbconsole: skip fb_flush when processing escape sequences Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 08/20] fbconsole: factor out helpers for clamped cursor row/col setting Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 09/20] fbconsole: precompute foreground/background colors Ahmad Fatoum
2026-05-03  8:33 ` Ahmad Fatoum [this message]
2026-05-03  8:33 ` [PATCH 11/20] fbconsole: implement CSI A/B/C/D cursor movement sequences Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 12/20] fbconsole: restrict cursor visibility to DEC private mode 25 Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 13/20] fbconsole: add new clear_chars helper Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 14/20] fbconsole: implement erase entire line CSI Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 15/20] fbconsole: support ESC[0J and ESC[1J partial screen clear Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 16/20] fbconsole: implement DEC save/restore cursor Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 17/20] fbconsole: implement VT100 deferred wrap (last column flag) Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 18/20] fbconsole: implement alternate screen buffer (ESC[?1049h/l) Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 19/20] fbconsole: adapt logging depending on activated streams Ahmad Fatoum
2026-05-03  8:33 ` [PATCH 20/20] netconsole: suppress log message when opening console file Ahmad Fatoum
2026-05-07 10:38 ` [PATCH 00/20] fbconsole: support TUI-relevant escape sequences Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260503084430.2765761-11-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox