Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/congfx/src/console.c

    r0d62c10 rbb14312  
    11/*
    2  * Copyright (c) 2019 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4848#include "../private/color.h"
    4949
     50static errno_t console_gc_set_clip_rect(void *, gfx_rect_t *);
    5051static errno_t console_gc_set_color(void *, gfx_color_t *);
    5152static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
     53static errno_t console_gc_update(void *);
    5254static errno_t console_gc_bitmap_create(void *, gfx_bitmap_params_t *,
    5355    gfx_bitmap_alloc_t *, void **);
     
    5557static errno_t console_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
    5658static errno_t console_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     59static errno_t console_gc_cursor_get_pos(void *, gfx_coord2_t *);
     60static errno_t console_gc_cursor_set_pos(void *, gfx_coord2_t *);
     61static errno_t console_gc_cursor_set_visible(void *, bool);
    5762
    5863gfx_context_ops_t console_gc_ops = {
     64        .set_clip_rect = console_gc_set_clip_rect,
    5965        .set_color = console_gc_set_color,
    6066        .fill_rect = console_gc_fill_rect,
     67        .update = console_gc_update,
    6168        .bitmap_create = console_gc_bitmap_create,
    6269        .bitmap_destroy = console_gc_bitmap_destroy,
    6370        .bitmap_render = console_gc_bitmap_render,
    64         .bitmap_get_alloc = console_gc_bitmap_get_alloc
     71        .bitmap_get_alloc = console_gc_bitmap_get_alloc,
     72        .cursor_get_pos = console_gc_cursor_get_pos,
     73        .cursor_set_pos = console_gc_cursor_set_pos,
     74        .cursor_set_visible = console_gc_cursor_set_visible
    6575};
    6676
     77/** Set clipping rectangle on console GC.
     78 *
     79 * @param arg Console GC
     80 * @param rect Rectangle
     81 *
     82 * @return EOK on success or an error code
     83 */
     84static errno_t console_gc_set_clip_rect(void *arg, gfx_rect_t *rect)
     85{
     86        console_gc_t *cgc = (console_gc_t *) arg;
     87
     88        if (rect != NULL)
     89                gfx_rect_clip(rect, &cgc->rect, &cgc->clip_rect);
     90        else
     91                cgc->clip_rect = cgc->rect;
     92
     93        return EOK;
     94}
     95
    6796/** Set color on console GC.
    6897 *
     
    92121{
    93122        console_gc_t *cgc = (console_gc_t *) arg;
    94         int rv;
    95123        gfx_coord_t x, y;
    96 
    97         // XXX We should handle p0.x > p1.x and p0.y > p1.y
    98 
    99         console_set_rgb_color(cgc->con, cgc->clr, cgc->clr);
    100 
    101         for (y = rect->p0.y; y < rect->p1.y; y++) {
    102                 console_set_pos(cgc->con, rect->p0.x, y);
    103 
    104                 for (x = rect->p0.x; x < rect->p1.x; x++) {
    105                         rv = fputc('X', cgc->fout);
    106                         if (rv < 0)
    107                                 return EIO;
     124        gfx_coord_t cols;
     125        gfx_rect_t crect;
     126        charfield_t ch;
     127
     128        /* Make sure rectangle is clipped and sorted */
     129        gfx_rect_clip(rect, &cgc->clip_rect, &crect);
     130
     131        cols = cgc->rect.p1.x - cgc->rect.p0.x;
     132
     133        ch.ch = cgc->clr >> 24;
     134        ch.flags = CHAR_FLAG_DIRTY;
     135        ch.attrs.type = CHAR_ATTR_RGB;
     136        ch.attrs.val.rgb.fgcolor = cgc->clr ^ 0xffffff;
     137        ch.attrs.val.rgb.bgcolor = cgc->clr;
     138
     139        for (y = crect.p0.y; y < crect.p1.y; y++) {
     140                for (x = crect.p0.x; x < crect.p1.x; x++) {
     141                        cgc->buf[y * cols + x] = ch;
    108142                }
    109 
    110                 console_flush(cgc->con);
    111143        }
    112144
     145        console_update(cgc->con, crect.p0.x, crect.p0.y,
     146            crect.p1.x, crect.p1.y);
     147
     148        return EOK;
     149}
     150
     151/** Update console GC.
     152 *
     153 * @param arg Console GC
     154 *
     155 * @return EOK on success or an error code
     156 */
     157static errno_t console_gc_update(void *arg)
     158{
     159        console_gc_t *cgc = (console_gc_t *) arg;
     160
     161        /*
     162         * XXX Before actually deferring update to here (and similarly other
     163         * GC implementations) need to make sure all consumers properly
     164         * call update.
     165         */
     166        (void) cgc;
    113167        return EOK;
    114168}
     
    131185        sysarg_t rows;
    132186        sysarg_t cols;
     187        charfield_t *buf = NULL;
    133188        errno_t rc;
    134189
     
    140195
    141196        rc = console_get_size(con, &cols, &rows);
     197        if (rc != EOK)
     198                goto error;
     199
     200        console_clear(con);
     201
     202        rc = console_map(con, cols, rows, &buf);
    142203        if (rc != EOK)
    143204                goto error;
     
    153214        cgc->rect.p0.y = 0;
    154215        cgc->rect.p1.x = cols;
    155         cgc->rect.p1.y = rows - 1; /* make sure we avoid bottom-right corner */
     216        cgc->rect.p1.y = rows;
     217        cgc->clip_rect = cgc->rect;
     218        cgc->buf = buf;
    156219
    157220        *rgc = cgc;
    158221        return EOK;
    159222error:
     223        if (buf != NULL)
     224                console_unmap(cgc->con, buf);
    160225        if (cgc != NULL)
    161226                free(cgc);
     
    175240        if (rc != EOK)
    176241                return rc;
     242
     243        console_clear(cgc->con);
     244        console_unmap(cgc->con, cgc->buf);
    177245
    178246        free(cgc);
     
    268336        console_gc_bitmap_t *cbm = (console_gc_bitmap_t *)bm;
    269337        gfx_coord_t x, y;
    270         int rv;
    271338        pixel_t clr;
     339        charfield_t ch;
    272340        pixelmap_t pixelmap;
    273341        gfx_rect_t srect;
     
    275343        gfx_rect_t crect;
    276344        gfx_coord2_t offs;
     345        gfx_coord_t cols;
    277346
    278347        if (srect0 != NULL)
     
    289358
    290359        gfx_rect_translate(&offs, &srect, &drect);
    291         gfx_rect_clip(&drect, &cbm->cgc->rect, &crect);
     360        gfx_rect_clip(&drect, &cbm->cgc->clip_rect, &crect);
    292361
    293362        pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
     
    295364        pixelmap.data = cbm->alloc.pixels;
    296365
     366        cols = cbm->cgc->rect.p1.x - cbm->cgc->rect.p0.x;
     367
    297368        if ((cbm->flags & bmpf_color_key) == 0) {
    298369                /* Simple copy */
    299370                for (y = crect.p0.y; y < crect.p1.y; y++) {
    300                         console_set_pos(cbm->cgc->con, crect.p0.x, y);
    301 
    302371                        for (x = crect.p0.x; x < crect.p1.x; x++) {
    303372                                clr = pixelmap_get_pixel(&pixelmap,
    304373                                    x - offs.x - cbm->rect.p0.x,
    305374                                    y - offs.y - cbm->rect.p0.y);
    306                                 console_set_rgb_color(cbm->cgc->con, clr, clr);
    307 
    308                                 rv = fputc('X', cbm->cgc->fout);
    309                                 if (rv < 0)
    310                                         return EIO;
    311 
    312                                 console_flush(cbm->cgc->con);
     375
     376                                ch.ch = clr >> 24;
     377                                ch.flags = CHAR_FLAG_DIRTY;
     378                                ch.attrs.type = CHAR_ATTR_RGB;
     379                                ch.attrs.val.rgb.fgcolor = clr ^ 0xffffff;
     380                                ch.attrs.val.rgb.bgcolor = clr;
     381
     382                                cbm->cgc->buf[y * cols + x] = ch;
    313383                        }
    314384                }
     
    321391                                    x - offs.x - cbm->rect.p0.x,
    322392                                    y - offs.y - cbm->rect.p0.y);
    323                                 console_set_rgb_color(cbm->cgc->con, clr, clr);
    324 
    325                                 if (clr != cbm->key_color) {
    326                                         console_set_pos(cbm->cgc->con, x, y);
    327                                         rv = fputc('X', cbm->cgc->fout);
    328                                         if (rv < 0)
    329                                                 return EIO;
    330 
    331                                         console_flush(cbm->cgc->con);
    332                                 }
    333 
     393
     394                                ch.ch = clr >> 24;
     395                                ch.flags = CHAR_FLAG_DIRTY;
     396                                ch.attrs.type = CHAR_ATTR_RGB;
     397                                ch.attrs.val.rgb.fgcolor = clr ^ 0xffffff;
     398                                ch.attrs.val.rgb.bgcolor = clr;
     399
     400                                if (clr != cbm->key_color)
     401                                        cbm->cgc->buf[y * cols + x] = ch;
    334402                        }
    335403                }
    336404        } else {
    337405                /* Color key & colorize */
    338                 console_set_rgb_color(cbm->cgc->con, cbm->cgc->clr,
    339                     cbm->cgc->clr);
     406                ch.ch = 0;
     407                ch.flags = CHAR_FLAG_DIRTY;
     408                ch.attrs.type = CHAR_ATTR_RGB;
     409                ch.attrs.val.rgb.fgcolor = cbm->cgc->clr;
     410                ch.attrs.val.rgb.bgcolor = cbm->cgc->clr;
    340411
    341412                for (y = crect.p0.y; y < crect.p1.y; y++) {
    342413                        for (x = crect.p0.x; x < crect.p1.x; x++) {
    343 
    344414                                clr = pixelmap_get_pixel(&pixelmap,
    345415                                    x - offs.x - cbm->rect.p0.x,
    346416                                    y - offs.y - cbm->rect.p0.y);
    347417
    348                                 if (clr != cbm->key_color) {
    349                                         console_set_pos(cbm->cgc->con, x, y);
    350                                         rv = fputc('X', cbm->cgc->fout);
    351                                         if (rv < 0)
    352                                                 return EIO;
    353 
    354                                         console_flush(cbm->cgc->con);
    355                                 }
    356 
     418                                if (clr != cbm->key_color)
     419                                        cbm->cgc->buf[y * cols + x] = ch;
    357420                        }
    358421                }
    359422        }
    360423
     424        console_update(cbm->cgc->con, crect.p0.x, crect.p0.y, crect.p1.x,
     425            crect.p1.y);
     426
    361427        return EOK;
    362428}
     
    375441}
    376442
     443/** Get cursor position on console GC.
     444 *
     445 * @param arg Console GC
     446 * @param pos Place to store position
     447 *
     448 * @return EOK on success or an error code
     449 */
     450static errno_t console_gc_cursor_get_pos(void *arg, gfx_coord2_t *pos)
     451{
     452        console_gc_t *cgc = (console_gc_t *) arg;
     453        sysarg_t col;
     454        sysarg_t row;
     455        errno_t rc;
     456
     457        rc = console_get_pos(cgc->con, &col, &row);
     458        if (rc != EOK)
     459                return rc;
     460
     461        pos->x = col;
     462        pos->y = row;
     463        return EOK;
     464}
     465
     466/** Set cursor position on console GC.
     467 *
     468 * @param arg Console GC
     469 * @param pos New cursor position
     470 *
     471 * @return EOK on success or an error code
     472 */
     473static errno_t console_gc_cursor_set_pos(void *arg, gfx_coord2_t *pos)
     474{
     475        console_gc_t *cgc = (console_gc_t *) arg;
     476
     477        console_set_pos(cgc->con, pos->x, pos->y);
     478        return EOK;
     479}
     480
     481/** Set cursor visibility on console GC.
     482 *
     483 * @param arg Console GC
     484 * @param visible @c true iff cursor should be made visible
     485 *
     486 * @return EOK on success or an error code
     487 */
     488static errno_t console_gc_cursor_set_visible(void *arg, bool visible)
     489{
     490        console_gc_t *cgc = (console_gc_t *) arg;
     491
     492        console_cursor_visibility(cgc->con, visible);
     493        return EOK;
     494}
     495
    377496/** @}
    378497 */
Note: See TracChangeset for help on using the changeset viewer.