diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/pc80/vga/vga.c | 46 | ||||
-rw-r--r-- | src/include/pc80/vga.h | 15 |
2 files changed, 54 insertions, 7 deletions
diff --git a/src/drivers/pc80/vga/vga.c b/src/drivers/pc80/vga/vga.c index 3471c944a1..a52ba6dc1f 100644 --- a/src/drivers/pc80/vga/vga.c +++ b/src/drivers/pc80/vga/vga.c @@ -256,19 +256,16 @@ vga_frame_set(unsigned int line, unsigned int character) vga_cr_write(0x0D, offset & 0xFF); } -/* - * simply fills a line with the given string. - */ -void -vga_line_write(unsigned int line, const char *string) +static void +vga_write_at_offset(unsigned int line, unsigned int offset, const char *string) { if (!string) return; - unsigned short *p = (unsigned short *)VGA_FB + (VGA_COLUMNS * line); + unsigned short *p = (unsigned short *)VGA_FB + (VGA_COLUMNS * line) + offset; size_t i, len = strlen(string); - for (i = 0; i < VGA_COLUMNS; i++) { + for (i = 0; i < (VGA_COLUMNS - offset); i++) { if (i < len) p[i] = 0x0F00 | string[i]; else @@ -277,6 +274,41 @@ vga_line_write(unsigned int line, const char *string) } /* + * simply fills a line with the given string. + */ +void +vga_line_write(unsigned int line, const char *string) +{ + vga_write_at_offset(line, 0, string); +} + +void +vga_write_text(enum VGA_TEXT_ALIGNMENT alignment, unsigned int line, const char *string) +{ + char str[VGA_COLUMNS * VGA_LINES] = {0}; + memcpy(str, string, strnlen(string, sizeof(str) - 1)); + + char *token = strtok(str, "\n"); + + while (token != NULL) { + size_t offset = VGA_COLUMNS - strnlen(token, VGA_COLUMNS); + switch (alignment) { + case VGA_TEXT_CENTER: + vga_write_at_offset(line++, offset/2, token); + break; + case VGA_TEXT_RIGHT: + vga_write_at_offset(line++, offset, token); + break; + case VGA_TEXT_LEFT: + default: + vga_write_at_offset(line++, 0, token); + break; + } + token = strtok(NULL, "\n"); + } +} + +/* * set up everything to get a basic 80x25 textmode. */ void diff --git a/src/include/pc80/vga.h b/src/include/pc80/vga.h index 4b7b26c36c..ec012f5bdf 100644 --- a/src/include/pc80/vga.h +++ b/src/include/pc80/vga.h @@ -8,6 +8,15 @@ #define VGA_COLUMNS 80 #define VGA_LINES 25 +#define VGA_TEXT_HORIZONTAL_TOP 0 +#define VGA_TEXT_HORIZONTAL_MIDDLE (VGA_LINES / 2) + +enum VGA_TEXT_ALIGNMENT { + VGA_TEXT_LEFT, + VGA_TEXT_CENTER, + VGA_TEXT_RIGHT, +}; + void vga_io_init(void); void vga_textmode_init(void); @@ -20,4 +29,10 @@ void vga_frame_set(unsigned int line, unsigned int character); void vga_line_write(unsigned int line, const char *string); +/* + * vga_write_text() writes a line of text aligned left/center/right + * horizontally on the screen (i.e. enum VGA_TEXT_ALIGNMENT) + */ +void vga_write_text(enum VGA_TEXT_ALIGNMENT alignment, unsigned int line, const char *string); + #endif /* VGA_H */ |