HelenOS sources
This source file includes following definitions.
- chargrid_create
- chargrid_destroy
- chargrid_cursor_at
- chargrid_get_top_row
- chargrid_update_rows
- chargrid_update_cols
- chargrid_putuchar
- chargrid_newline
- chargrid_tabstop
- chargrid_backspace
- chargrid_clear
- chargrid_set_cursor
- chargrid_set_cursor_visibility
- chargrid_get_cursor
- chargrid_get_cursor_visibility
- chargrid_clear_row
- chargrid_set_style
- chargrid_set_color
- chargrid_set_rgb_color
#include <io/style.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <as.h>
#include <io/chargrid.h>
chargrid_t *chargrid_create(sysarg_t cols, sysarg_t rows,
chargrid_flag_t flags)
{
size_t size =
sizeof(chargrid_t) + cols * rows * sizeof(charfield_t);
chargrid_t *scrbuf;
if ((flags & CHARGRID_FLAG_SHARED) == CHARGRID_FLAG_SHARED) {
scrbuf = (chargrid_t *) as_area_create(AS_AREA_ANY, size,
AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
AS_AREA_UNPAGED);
if (scrbuf == AS_MAP_FAILED)
return NULL;
} else {
scrbuf = (chargrid_t *) malloc(size);
if (scrbuf == NULL)
return NULL;
}
scrbuf->size = size;
scrbuf->flags = flags;
scrbuf->cols = cols;
scrbuf->rows = rows;
scrbuf->cursor_visible = false;
scrbuf->attrs.type = CHAR_ATTR_STYLE;
scrbuf->attrs.val.style = STYLE_NORMAL;
scrbuf->top_row = 0;
chargrid_clear(scrbuf);
return scrbuf;
}
void chargrid_destroy(chargrid_t *srcbuf)
{
}
bool chargrid_cursor_at(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
{
return ((scrbuf->cursor_visible) && (scrbuf->col == col) &&
(scrbuf->row == row));
}
sysarg_t chargrid_get_top_row(chargrid_t *scrbuf)
{
return scrbuf->top_row;
}
static sysarg_t chargrid_update_rows(chargrid_t *scrbuf)
{
if (scrbuf->row == scrbuf->rows) {
scrbuf->row = scrbuf->rows - 1;
scrbuf->top_row = (scrbuf->top_row + 1) % scrbuf->rows;
chargrid_clear_row(scrbuf, scrbuf->row);
return scrbuf->rows;
}
return 2;
}
static sysarg_t chargrid_update_cols(chargrid_t *scrbuf)
{
if (scrbuf->col == scrbuf->cols) {
scrbuf->col = 0;
scrbuf->row++;
return chargrid_update_rows(scrbuf);
}
return 1;
}
sysarg_t chargrid_putuchar(chargrid_t *scrbuf, char32_t ch, bool update)
{
assert(scrbuf->col < scrbuf->cols);
assert(scrbuf->row < scrbuf->rows);
charfield_t *field =
chargrid_charfield_at(scrbuf, scrbuf->col, scrbuf->row);
field->ch = ch;
field->attrs = scrbuf->attrs;
field->flags |= CHAR_FLAG_DIRTY;
if (update) {
scrbuf->col++;
return chargrid_update_cols(scrbuf);
}
return 1;
}
sysarg_t chargrid_newline(chargrid_t *scrbuf)
{
assert(scrbuf->col < scrbuf->cols);
assert(scrbuf->row < scrbuf->rows);
scrbuf->col = 0;
scrbuf->row++;
return chargrid_update_rows(scrbuf);
}
sysarg_t chargrid_tabstop(chargrid_t *scrbuf, sysarg_t tab_size)
{
assert(scrbuf->col < scrbuf->cols);
assert(scrbuf->row < scrbuf->rows);
sysarg_t spaces = tab_size - scrbuf->cols % tab_size;
sysarg_t flush = 1;
for (sysarg_t i = 0; i < spaces; i++)
flush += chargrid_putuchar(scrbuf, ' ', true) - 1;
return flush;
}
sysarg_t chargrid_backspace(chargrid_t *scrbuf)
{
assert(scrbuf->col < scrbuf->cols);
assert(scrbuf->row < scrbuf->rows);
if ((scrbuf->col == 0) && (scrbuf->row == 0))
return 0;
if (scrbuf->col == 0) {
scrbuf->col = scrbuf->cols - 1;
scrbuf->row--;
chargrid_putuchar(scrbuf, ' ', false);
return 2;
}
scrbuf->col--;
chargrid_putuchar(scrbuf, ' ', false);
return 1;
}
void chargrid_clear(chargrid_t *scrbuf)
{
for (size_t pos = 0; pos < (scrbuf->cols * scrbuf->rows); pos++) {
scrbuf->data[pos].ch = 0;
scrbuf->data[pos].attrs = scrbuf->attrs;
scrbuf->data[pos].flags = CHAR_FLAG_DIRTY;
}
scrbuf->col = 0;
scrbuf->row = 0;
}
void chargrid_set_cursor(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
{
if (col >= scrbuf->cols || row >= scrbuf->rows)
return;
scrbuf->col = col;
scrbuf->row = row;
}
void chargrid_set_cursor_visibility(chargrid_t *scrbuf, bool visible)
{
scrbuf->cursor_visible = visible;
}
void chargrid_get_cursor(chargrid_t *scrbuf, sysarg_t *col,
sysarg_t *row)
{
assert(col);
assert(row);
*col = scrbuf->col;
*row = scrbuf->row;
}
bool chargrid_get_cursor_visibility(chargrid_t *scrbuf)
{
return scrbuf->cursor_visible;
}
void chargrid_clear_row(chargrid_t *scrbuf, sysarg_t row)
{
for (sysarg_t col = 0; col < scrbuf->cols; col++) {
charfield_t *field =
chargrid_charfield_at(scrbuf, col, row);
field->ch = 0;
field->attrs = scrbuf->attrs;
field->flags |= CHAR_FLAG_DIRTY;
}
}
void chargrid_set_style(chargrid_t *scrbuf, console_style_t style)
{
scrbuf->attrs.type = CHAR_ATTR_STYLE;
scrbuf->attrs.val.style = style;
}
void chargrid_set_color(chargrid_t *scrbuf, console_color_t bgcolor,
console_color_t fgcolor, console_color_attr_t attr)
{
scrbuf->attrs.type = CHAR_ATTR_INDEX;
scrbuf->attrs.val.index.bgcolor = bgcolor;
scrbuf->attrs.val.index.fgcolor = fgcolor;
scrbuf->attrs.val.index.attr = attr;
}
void chargrid_set_rgb_color(chargrid_t *scrbuf, pixel_t bgcolor,
pixel_t fgcolor)
{
scrbuf->attrs.type = CHAR_ATTR_RGB;
scrbuf->attrs.val.rgb.bgcolor = bgcolor;
scrbuf->attrs.val.rgb.fgcolor = fgcolor;
}
HelenOS homepage, sources at GitHub