Changeset d3109ff in mainline for uspace/lib/vt/src/vt100.c


Ignore:
Timestamp:
2024-09-24T17:59:36Z (2 weeks ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master
Children:
6a753a9c
Parents:
3fcea34
Message:

Cursor and color control in remote console + RGB

Move vt100 module from output server into separate vt library
Add cursor and color control to remcons using libvt
Add RGB color control to serial console and remote console

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/vt/src/vt100.c

    r3fcea34 rd3109ff  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * Copyright (c) 2011 Martin Decky
    44 * All rights reserved.
     
    2828 */
    2929
    30 /** @addtogroup output
     30/** @addtogroup libvt
    3131 * @{
    3232 */
    3333
    34 #include <inttypes.h>
    3534#include <errno.h>
    36 #include <stddef.h>
     35#include <io/color.h>
    3736#include <stdio.h>
    3837#include <stdlib.h>
    39 #include <io/color.h>
    40 #include <types/common.h>
    41 #include "vt100.h"
    42 
    43 /** Buffer size when creating actual VT100 commands.
    44  *
    45  * This is absurdly large but since we accept numbers via sysarg_t,
    46  * we make it big enough for the largest value to be on the safe side
    47  * (and to silence compiler too).
    48  *
    49  * TODO: find out if VT100 has some hard limits or perhaps simply cut-out
    50  * values larger than 16 bits or something.
    51  */
    52 #define MAX_CONTROL 64
    53 
    54 typedef enum {
    55         CI_BLACK   = 0,
    56         CI_RED     = 1,
    57         CI_GREEN   = 2,
    58         CI_BROWN   = 3,
    59         CI_BLUE    = 4,
    60         CI_MAGENTA = 5,
    61         CI_CYAN    = 6,
    62         CI_WHITE   = 7
    63 } sgr_color_index_t;
    64 
    65 typedef enum {
    66         SGR_RESET       = 0,
    67         SGR_BOLD        = 1,
    68         SGR_UNDERLINE   = 4,
    69         SGR_BLINK       = 5,
    70         SGR_REVERSE     = 7,
    71         SGR_FGCOLOR     = 30,
    72         SGR_BGCOLOR     = 40
    73 } sgr_command_t;
    74 
    75 static sgr_color_index_t color_map[] = {
     38#include <vt/vt100.h>
     39
     40sgr_color_index_t color_map[] = {
    7641        [COLOR_BLACK]   = CI_BLACK,
    7742        [COLOR_BLUE]    = CI_BLUE,
     
    8449};
    8550
     51void vt100_cls(vt100_state_t *state)
     52{
     53        state->control_puts(state->arg, "\033[2J");
     54}
     55
    8656/** ECMA-48 Set Graphics Rendition. */
    8757static void vt100_sgr(vt100_state_t *state, unsigned int mode)
     
    9060
    9161        snprintf(control, MAX_CONTROL, "\033[%um", mode);
    92         state->control_puts(control);
    93 }
    94 
    95 static void vt100_set_pos(vt100_state_t *state, sysarg_t col, sysarg_t row)
     62        state->control_puts(state->arg, control);
     63}
     64
     65/** Set Graphics Rendition with 5 arguments. */
     66static void vt100_sgr5(vt100_state_t *state, unsigned a1, unsigned a2,
     67    unsigned a3, unsigned a4, unsigned a5)
     68{
     69        char control[MAX_CONTROL];
     70
     71        snprintf(control, MAX_CONTROL, "\033[%u;%u;%u;%u;%um",
     72            a1, a2, a3, a4, a5);
     73        state->control_puts(state->arg, control);
     74}
     75
     76void vt100_set_pos(vt100_state_t *state, sysarg_t col, sysarg_t row)
    9677{
    9778        char control[MAX_CONTROL];
     
    9980        snprintf(control, MAX_CONTROL, "\033[%" PRIun ";%" PRIun "f",
    10081            row + 1, col + 1);
    101         state->control_puts(control);
    102 }
    103 
    104 static void vt100_set_sgr(vt100_state_t *state, char_attrs_t attrs)
    105 {
     82        state->control_puts(state->arg, control);
     83}
     84
     85void vt100_set_sgr(vt100_state_t *state, char_attrs_t attrs)
     86{
     87        unsigned color;
     88
    10689        switch (attrs.type) {
    10790        case CHAR_ATTR_STYLE:
     
    140123                break;
    141124        case CHAR_ATTR_RGB:
    142                 vt100_sgr(state, SGR_RESET);
    143 
    144                 if (attrs.val.rgb.bgcolor <= attrs.val.rgb.fgcolor)
    145                         vt100_sgr(state, SGR_REVERSE);
    146 
     125                if (state->enable_rgb == true) {
     126                        vt100_sgr5(state, 48, 2, RED(attrs.val.rgb.bgcolor),
     127                            GREEN(attrs.val.rgb.bgcolor),
     128                            BLUE(attrs.val.rgb.bgcolor));
     129                        vt100_sgr5(state, 38, 2, RED(attrs.val.rgb.fgcolor),
     130                            GREEN(attrs.val.rgb.fgcolor),
     131                            BLUE(attrs.val.rgb.fgcolor));
     132                } else {
     133                        vt100_sgr(state, SGR_RESET);
     134                        color =
     135                            ((RED(attrs.val.rgb.fgcolor) >= 0x80) ? COLOR_RED : 0) |
     136                            ((GREEN(attrs.val.rgb.fgcolor) >= 0x80) ? COLOR_GREEN : 0) |
     137                            ((BLUE(attrs.val.rgb.fgcolor) >= 0x80) ? COLOR_BLUE : 0);
     138                        vt100_sgr(state, SGR_FGCOLOR + color_map[color]);
     139                        color =
     140                            ((RED(attrs.val.rgb.bgcolor) >= 0x80) ? COLOR_RED : 0) |
     141                            ((GREEN(attrs.val.rgb.bgcolor) >= 0x80) ? COLOR_GREEN : 0) |
     142                            ((BLUE(attrs.val.rgb.bgcolor) >= 0x80) ? COLOR_BLUE : 0);
     143                        vt100_sgr(state, SGR_BGCOLOR + color_map[color]);
     144                }
    147145                break;
    148146        }
    149147}
    150148
    151 vt100_state_t *vt100_state_create(sysarg_t cols, sysarg_t rows,
     149vt100_state_t *vt100_state_create(void *arg, sysarg_t cols, sysarg_t rows,
    152150    vt100_putuchar_t putuchar_fn, vt100_control_puts_t control_puts_fn,
    153151    vt100_flush_t flush_fn)
     
    157155                return NULL;
    158156
     157        state->arg = arg;
    159158        state->putuchar = putuchar_fn;
    160159        state->control_puts = control_puts_fn;
     
    169168        state->cur_attrs.type = CHAR_ATTR_STYLE;
    170169        state->cur_attrs.val.style = STYLE_NORMAL;
    171 
    172         /* Initialize graphic rendition attributes */
    173         vt100_sgr(state, SGR_RESET);
    174         vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
    175         vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
    176         state->control_puts("\033[2J");
    177         state->control_puts("\033[?25l");
    178170
    179171        return state;
     
    225217{
    226218        if (visible)
    227                 state->control_puts("\033[?25h");
     219                state->control_puts(state->arg, "\033[?25h");
    228220        else
    229                 state->control_puts("\033[?25l");
     221                state->control_puts(state->arg, "\033[?25l");
    230222}
    231223
    232224void vt100_putuchar(vt100_state_t *state, char32_t ch)
    233225{
    234         state->putuchar(ch == 0 ? ' ' : ch);
     226        state->putuchar(state->arg, ch == 0 ? ' ' : ch);
    235227        state->cur_col++;
    236228
     
    243235void vt100_flush(vt100_state_t *state)
    244236{
    245         state->flush();
     237        state->flush(state->arg);
    246238}
    247239
Note: See TracChangeset for help on using the changeset viewer.