Skip to content

Commit 25ca9ee

Browse files
committed
drivers/vga_textmode: Defer line wrap to next putchar
1 parent ef9b025 commit 25ca9ee

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

common/drivers/vga_textmode.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static void text_set_cursor_pos(struct flanterm_context *_ctx, size_t x, size_t
162162
}
163163
}
164164
ctx->cursor_offset = y * VD_COLS + x * 2;
165+
ctx->cursor_overflow = false;
165166
}
166167

167168
static uint8_t ansi_colours[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
@@ -219,17 +220,32 @@ static void text_set_text_bg_default_bright(struct flanterm_context *_ctx) {
219220
static void text_putchar(struct flanterm_context *_ctx, uint8_t c) {
220221
struct textmode_context *ctx = (void *)_ctx;
221222

222-
ctx->back_buffer[ctx->cursor_offset] = c;
223-
ctx->back_buffer[ctx->cursor_offset + 1] = ctx->text_palette;
224-
if (ctx->cursor_offset / VD_COLS == _ctx->scroll_bottom_margin - 1
225-
&& ctx->cursor_offset % VD_COLS == VD_COLS - 2) {
226-
if (_ctx->scroll_enabled) {
227-
text_scroll(_ctx);
223+
// Handle overflow from previous putchar
224+
if (ctx->cursor_overflow) {
225+
ctx->cursor_overflow = false;
226+
if (_ctx->wrap_enabled
227+
&& (ctx->cursor_offset / VD_COLS < _ctx->scroll_bottom_margin - 1
228+
|| _ctx->scroll_enabled)) {
228229
ctx->cursor_offset -= ctx->cursor_offset % VD_COLS;
230+
ctx->cursor_offset += VD_COLS;
231+
if (ctx->cursor_offset / VD_COLS == _ctx->scroll_bottom_margin) {
232+
ctx->cursor_offset -= VD_COLS;
233+
text_scroll(_ctx);
234+
}
235+
if (ctx->cursor_offset >= VD_ROWS * VD_COLS) {
236+
ctx->cursor_offset = (VD_ROWS - 1) * VD_COLS;
237+
}
238+
} else {
239+
ctx->cursor_offset = ctx->cursor_offset - (ctx->cursor_offset % VD_COLS) + VD_COLS - 2;
229240
}
230-
} else if (ctx->cursor_offset >= (VIDEO_BOTTOM - 1)) {
231-
ctx->cursor_offset -= ctx->cursor_offset % VD_COLS;
232-
} else {
241+
}
242+
243+
ctx->back_buffer[ctx->cursor_offset] = c;
244+
ctx->back_buffer[ctx->cursor_offset + 1] = ctx->text_palette;
245+
if (ctx->cursor_offset % VD_COLS == VD_COLS - 2) {
246+
// At last column - flag overflow for next putchar
247+
ctx->cursor_overflow = true;
248+
} else if (ctx->cursor_offset < (VIDEO_BOTTOM - 1)) {
233249
ctx->cursor_offset += 2;
234250
}
235251
}
@@ -285,6 +301,7 @@ void vga_textmode_init(bool managed) {
285301
}
286302

287303
ctx->cursor_offset = 0;
304+
ctx->cursor_overflow = false;
288305
ctx->text_palette = 0x07;
289306

290307
ctx->video_mem = (volatile uint8_t *)0xb8000;

common/drivers/vga_textmode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct textmode_context {
2222
size_t cursor_offset;
2323
size_t old_cursor_offset;
2424
bool cursor_status;
25+
bool cursor_overflow;
2526
uint8_t text_palette;
2627

2728
uint8_t saved_state_text_palette;

0 commit comments

Comments
 (0)