HelenOS sources
This source file includes following definitions.
- console_gc_pix_to_charfield
- console_gc_set_clip_rect
- console_gc_set_color
- console_gc_fill_rect
- console_gc_update
- console_gc_create
- console_gc_delete
- console_gc_suspend
- console_gc_resume
- console_gc_get_ctx
- console_gc_bitmap_create
- console_gc_bitmap_destroy
- console_gc_bitmap_render
- console_gc_bitmap_get_alloc
- console_gc_cursor_get_pos
- console_gc_cursor_set_pos
- console_gc_cursor_set_visible
#include <congfx/console.h>
#include <gfx/context.h>
#include <gfx/coord.h>
#include <gfx/render.h>
#include <io/pixel.h>
#include <io/pixelmap.h>
#include <stdlib.h>
#include "../private/console.h"
#include "../private/color.h"
static errno_t console_gc_set_clip_rect(void *, gfx_rect_t *);
static errno_t console_gc_set_color(void *, gfx_color_t *);
static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
static errno_t console_gc_update(void *);
static errno_t console_gc_bitmap_create(void *, gfx_bitmap_params_t *,
gfx_bitmap_alloc_t *, void **);
static errno_t console_gc_bitmap_destroy(void *);
static errno_t console_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
static errno_t console_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
static errno_t console_gc_cursor_get_pos(void *, gfx_coord2_t *);
static errno_t console_gc_cursor_set_pos(void *, gfx_coord2_t *);
static errno_t console_gc_cursor_set_visible(void *, bool);
gfx_context_ops_t console_gc_ops = {
.set_clip_rect = console_gc_set_clip_rect,
.set_color = console_gc_set_color,
.fill_rect = console_gc_fill_rect,
.update = console_gc_update,
.bitmap_create = console_gc_bitmap_create,
.bitmap_destroy = console_gc_bitmap_destroy,
.bitmap_render = console_gc_bitmap_render,
.bitmap_get_alloc = console_gc_bitmap_get_alloc,
.cursor_get_pos = console_gc_cursor_get_pos,
.cursor_set_pos = console_gc_cursor_set_pos,
.cursor_set_visible = console_gc_cursor_set_visible
};
static void console_gc_pix_to_charfield(pixel_t clr, charfield_t *ch)
{
uint8_t attr;
if ((clr >> 24) == 0xff) {
ch->ch = 0;
ch->flags = CHAR_FLAG_DIRTY;
ch->attrs.type = CHAR_ATTR_RGB;
ch->attrs.val.rgb.fgcolor = clr;
ch->attrs.val.rgb.bgcolor = clr;
} else {
attr = clr >> 24;
ch->ch = clr & 0xffffff;
ch->flags = CHAR_FLAG_DIRTY;
ch->attrs.type = CHAR_ATTR_INDEX;
ch->attrs.val.index.fgcolor = attr & 0x7;
ch->attrs.val.index.bgcolor = (attr >> 4) & 0x7;
ch->attrs.val.index.attr =
((attr & 0x8) ? CATTR_BRIGHT : 0) +
((attr & 0x80) ? CATTR_BLINK : 0);
}
}
static errno_t console_gc_set_clip_rect(void *arg, gfx_rect_t *rect)
{
console_gc_t *cgc = (console_gc_t *) arg;
if (rect != NULL)
gfx_rect_clip(rect, &cgc->rect, &cgc->clip_rect);
else
cgc->clip_rect = cgc->rect;
return EOK;
}
static errno_t console_gc_set_color(void *arg, gfx_color_t *color)
{
console_gc_t *cgc = (console_gc_t *) arg;
cgc->clr = PIXEL(color->attr, color->r >> 8, color->g >> 8, color->b >> 8);
return EOK;
}
static errno_t console_gc_fill_rect(void *arg, gfx_rect_t *rect)
{
console_gc_t *cgc = (console_gc_t *) arg;
gfx_coord_t x, y;
gfx_coord_t cols;
gfx_rect_t crect;
charfield_t ch;
gfx_rect_clip(rect, &cgc->clip_rect, &crect);
cols = cgc->rect.p1.x - cgc->rect.p0.x;
console_gc_pix_to_charfield(cgc->clr, &ch);
for (y = crect.p0.y; y < crect.p1.y; y++) {
for (x = crect.p0.x; x < crect.p1.x; x++) {
cgc->buf[y * cols + x] = ch;
}
}
console_update(cgc->con, crect.p0.x, crect.p0.y,
crect.p1.x, crect.p1.y);
return EOK;
}
static errno_t console_gc_update(void *arg)
{
console_gc_t *cgc = (console_gc_t *) arg;
(void) cgc;
return EOK;
}
errno_t console_gc_create(console_ctrl_t *con, FILE *fout,
console_gc_t **rgc)
{
console_gc_t *cgc = NULL;
gfx_context_t *gc = NULL;
sysarg_t rows;
sysarg_t cols;
charfield_t *buf = NULL;
errno_t rc;
cgc = calloc(1, sizeof(console_gc_t));
if (cgc == NULL) {
rc = ENOMEM;
goto error;
}
rc = console_get_size(con, &cols, &rows);
if (rc != EOK)
goto error;
console_clear(con);
rc = console_map(con, cols, rows, &buf);
if (rc != EOK)
goto error;
rc = gfx_context_new(&console_gc_ops, cgc, &gc);
if (rc != EOK)
goto error;
cgc->gc = gc;
cgc->con = con;
cgc->fout = fout;
cgc->rect.p0.x = 0;
cgc->rect.p0.y = 0;
cgc->rect.p1.x = cols;
cgc->rect.p1.y = rows;
cgc->clip_rect = cgc->rect;
cgc->buf = buf;
*rgc = cgc;
return EOK;
error:
if (buf != NULL)
console_unmap(cgc->con, buf);
if (cgc != NULL)
free(cgc);
gfx_context_delete(gc);
return rc;
}
errno_t console_gc_delete(console_gc_t *cgc)
{
errno_t rc;
rc = gfx_context_delete(cgc->gc);
if (rc != EOK)
return rc;
console_clear(cgc->con);
console_unmap(cgc->con, cgc->buf);
free(cgc);
return EOK;
}
errno_t console_gc_suspend(console_gc_t *cgc)
{
console_unmap(cgc->con, cgc->buf);
cgc->buf = NULL;
console_clear(cgc->con);
console_cursor_visibility(cgc->con, true);
return EOK;
}
errno_t console_gc_resume(console_gc_t *cgc)
{
errno_t rc;
console_clear(cgc->con);
rc = console_map(cgc->con, cgc->rect.p1.x, cgc->rect.p1.y, &cgc->buf);
if (rc != EOK)
return rc;
return EOK;
}
gfx_context_t *console_gc_get_ctx(console_gc_t *cgc)
{
return cgc->gc;
}
errno_t console_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
gfx_bitmap_alloc_t *alloc, void **rbm)
{
console_gc_t *cgc = (console_gc_t *) arg;
console_gc_bitmap_t *cbm = NULL;
gfx_coord2_t dim;
errno_t rc;
if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
return ENOTSUP;
cbm = calloc(1, sizeof(console_gc_bitmap_t));
if (cbm == NULL)
return ENOMEM;
gfx_coord2_subtract(¶ms->rect.p1, ¶ms->rect.p0, &dim);
cbm->rect = params->rect;
cbm->flags = params->flags;
cbm->key_color = params->key_color;
if (alloc == NULL) {
cbm->alloc.pitch = dim.x * sizeof(uint32_t);
cbm->alloc.off0 = 0;
cbm->alloc.pixels = calloc(dim.x * dim.y, sizeof(uint32_t));
if (cbm->alloc.pixels == NULL) {
rc = ENOMEM;
goto error;
}
cbm->myalloc = true;
} else {
cbm->alloc = *alloc;
}
cbm->cgc = cgc;
*rbm = (void *)cbm;
return EOK;
error:
if (cbm != NULL)
free(cbm);
return rc;
}
static errno_t console_gc_bitmap_destroy(void *bm)
{
console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
if (cbm->myalloc)
free(cbm->alloc.pixels);
free(cbm);
return EOK;
}
static errno_t console_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
gfx_coord2_t *offs0)
{
console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
gfx_coord_t x, y;
pixel_t clr;
charfield_t ch;
pixelmap_t pixelmap;
gfx_rect_t srect;
gfx_rect_t drect;
gfx_rect_t crect;
gfx_coord2_t offs;
gfx_coord_t cols;
if (srect0 != NULL)
srect = *srect0;
else
srect = cbm->rect;
if (offs0 != NULL) {
offs = *offs0;
} else {
offs.x = 0;
offs.y = 0;
}
gfx_rect_translate(&offs, &srect, &drect);
gfx_rect_clip(&drect, &cbm->cgc->clip_rect, &crect);
pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
pixelmap.height = cbm->rect.p1.y = cbm->rect.p1.y;
pixelmap.data = cbm->alloc.pixels;
cols = cbm->cgc->rect.p1.x - cbm->cgc->rect.p0.x;
if ((cbm->flags & bmpf_color_key) == 0) {
for (y = crect.p0.y; y < crect.p1.y; y++) {
for (x = crect.p0.x; x < crect.p1.x; x++) {
clr = pixelmap_get_pixel(&pixelmap,
x - offs.x - cbm->rect.p0.x,
y - offs.y - cbm->rect.p0.y);
console_gc_pix_to_charfield(clr, &ch);
cbm->cgc->buf[y * cols + x] = ch;
}
}
} else if ((cbm->flags & bmpf_colorize) == 0) {
for (y = crect.p0.y; y < crect.p1.y; y++) {
for (x = crect.p0.x; x < crect.p1.x; x++) {
clr = pixelmap_get_pixel(&pixelmap,
x - offs.x - cbm->rect.p0.x,
y - offs.y - cbm->rect.p0.y);
console_gc_pix_to_charfield(clr, &ch);
if (clr != cbm->key_color)
cbm->cgc->buf[y * cols + x] = ch;
}
}
} else {
console_gc_pix_to_charfield(cbm->cgc->clr, &ch);
for (y = crect.p0.y; y < crect.p1.y; y++) {
for (x = crect.p0.x; x < crect.p1.x; x++) {
clr = pixelmap_get_pixel(&pixelmap,
x - offs.x - cbm->rect.p0.x,
y - offs.y - cbm->rect.p0.y);
if (clr != cbm->key_color)
cbm->cgc->buf[y * cols + x] = ch;
}
}
}
console_update(cbm->cgc->con, crect.p0.x, crect.p0.y, crect.p1.x,
crect.p1.y);
return EOK;
}
static errno_t console_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
{
console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
*alloc = cbm->alloc;
return EOK;
}
static errno_t console_gc_cursor_get_pos(void *arg, gfx_coord2_t *pos)
{
console_gc_t *cgc = (console_gc_t *) arg;
sysarg_t col;
sysarg_t row;
errno_t rc;
rc = console_get_pos(cgc->con, &col, &row);
if (rc != EOK)
return rc;
pos->x = col;
pos->y = row;
return EOK;
}
static errno_t console_gc_cursor_set_pos(void *arg, gfx_coord2_t *pos)
{
console_gc_t *cgc = (console_gc_t *) arg;
console_set_pos(cgc->con, pos->x, pos->y);
return EOK;
}
static errno_t console_gc_cursor_set_visible(void *arg, bool visible)
{
console_gc_t *cgc = (console_gc_t *) arg;
console_cursor_visibility(cgc->con, visible);
return EOK;
}
HelenOS homepage, sources at GitHub