[7c014d1] | 1 | /*
|
---|
[ead72f2] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[7c014d1] | 3 | * Copyright (c) 2011 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[4122410] | 30 | /** @addtogroup output
|
---|
| 31 | * @{
|
---|
[7c014d1] | 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <inttypes.h>
|
---|
[6d5e378] | 35 | #include <errno.h>
|
---|
[582a0b8] | 36 | #include <stddef.h>
|
---|
[38d150e] | 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[6d5e378] | 39 | #include <io/color.h>
|
---|
[cf13b17] | 40 | #include <types/common.h>
|
---|
[7c014d1] | 41 | #include "vt100.h"
|
---|
| 42 |
|
---|
| 43 | #define MAX_CONTROL 20
|
---|
| 44 |
|
---|
| 45 | typedef enum {
|
---|
| 46 | CI_BLACK = 0,
|
---|
| 47 | CI_RED = 1,
|
---|
| 48 | CI_GREEN = 2,
|
---|
| 49 | CI_BROWN = 3,
|
---|
| 50 | CI_BLUE = 4,
|
---|
| 51 | CI_MAGENTA = 5,
|
---|
| 52 | CI_CYAN = 6,
|
---|
| 53 | CI_WHITE = 7
|
---|
| 54 | } sgr_color_index_t;
|
---|
| 55 |
|
---|
| 56 | typedef enum {
|
---|
| 57 | SGR_RESET = 0,
|
---|
| 58 | SGR_BOLD = 1,
|
---|
| 59 | SGR_UNDERLINE = 4,
|
---|
| 60 | SGR_BLINK = 5,
|
---|
| 61 | SGR_REVERSE = 7,
|
---|
| 62 | SGR_FGCOLOR = 30,
|
---|
| 63 | SGR_BGCOLOR = 40
|
---|
| 64 | } sgr_command_t;
|
---|
| 65 |
|
---|
| 66 | static sgr_color_index_t color_map[] = {
|
---|
| 67 | [COLOR_BLACK] = CI_BLACK,
|
---|
[ead72f2] | 68 | [COLOR_BLUE] = CI_BLUE,
|
---|
[7c014d1] | 69 | [COLOR_GREEN] = CI_GREEN,
|
---|
| 70 | [COLOR_CYAN] = CI_CYAN,
|
---|
| 71 | [COLOR_RED] = CI_RED,
|
---|
| 72 | [COLOR_MAGENTA] = CI_MAGENTA,
|
---|
| 73 | [COLOR_YELLOW] = CI_BROWN,
|
---|
| 74 | [COLOR_WHITE] = CI_WHITE
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | /** ECMA-48 Set Graphics Rendition. */
|
---|
| 78 | static void vt100_sgr(vt100_state_t *state, unsigned int mode)
|
---|
| 79 | {
|
---|
| 80 | char control[MAX_CONTROL];
|
---|
[a35b458] | 81 |
|
---|
[7c014d1] | 82 | snprintf(control, MAX_CONTROL, "\033[%um", mode);
|
---|
| 83 | state->control_puts(control);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static void vt100_set_pos(vt100_state_t *state, sysarg_t col, sysarg_t row)
|
---|
| 87 | {
|
---|
| 88 | char control[MAX_CONTROL];
|
---|
[a35b458] | 89 |
|
---|
[7c014d1] | 90 | snprintf(control, MAX_CONTROL, "\033[%" PRIun ";%" PRIun "f",
|
---|
| 91 | row + 1, col + 1);
|
---|
| 92 | state->control_puts(control);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | static void vt100_set_sgr(vt100_state_t *state, char_attrs_t attrs)
|
---|
| 96 | {
|
---|
| 97 | switch (attrs.type) {
|
---|
| 98 | case CHAR_ATTR_STYLE:
|
---|
| 99 | switch (attrs.val.style) {
|
---|
| 100 | case STYLE_NORMAL:
|
---|
| 101 | vt100_sgr(state, SGR_RESET);
|
---|
| 102 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
| 103 | vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
|
---|
| 104 | break;
|
---|
| 105 | case STYLE_EMPHASIS:
|
---|
| 106 | vt100_sgr(state, SGR_RESET);
|
---|
| 107 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
| 108 | vt100_sgr(state, SGR_FGCOLOR + CI_RED);
|
---|
| 109 | vt100_sgr(state, SGR_BOLD);
|
---|
| 110 | break;
|
---|
| 111 | case STYLE_INVERTED:
|
---|
| 112 | vt100_sgr(state, SGR_RESET);
|
---|
| 113 | vt100_sgr(state, SGR_BGCOLOR + CI_BLACK);
|
---|
| 114 | vt100_sgr(state, SGR_FGCOLOR + CI_WHITE);
|
---|
| 115 | break;
|
---|
| 116 | case STYLE_SELECTED:
|
---|
| 117 | vt100_sgr(state, SGR_RESET);
|
---|
| 118 | vt100_sgr(state, SGR_BGCOLOR + CI_RED);
|
---|
| 119 | vt100_sgr(state, SGR_FGCOLOR + CI_WHITE);
|
---|
| 120 | break;
|
---|
| 121 | }
|
---|
| 122 | break;
|
---|
| 123 | case CHAR_ATTR_INDEX:
|
---|
| 124 | vt100_sgr(state, SGR_RESET);
|
---|
| 125 | vt100_sgr(state, SGR_BGCOLOR + color_map[attrs.val.index.bgcolor & 7]);
|
---|
| 126 | vt100_sgr(state, SGR_FGCOLOR + color_map[attrs.val.index.fgcolor & 7]);
|
---|
[a35b458] | 127 |
|
---|
[7c014d1] | 128 | if (attrs.val.index.attr & CATTR_BRIGHT)
|
---|
| 129 | vt100_sgr(state, SGR_BOLD);
|
---|
[a35b458] | 130 |
|
---|
[7c014d1] | 131 | break;
|
---|
| 132 | case CHAR_ATTR_RGB:
|
---|
| 133 | vt100_sgr(state, SGR_RESET);
|
---|
[a35b458] | 134 |
|
---|
[7c014d1] | 135 | if (attrs.val.rgb.bgcolor <= attrs.val.rgb.fgcolor)
|
---|
| 136 | vt100_sgr(state, SGR_REVERSE);
|
---|
[a35b458] | 137 |
|
---|
[7c014d1] | 138 | break;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[6d5e378] | 142 | vt100_state_t *vt100_state_create(sysarg_t cols, sysarg_t rows,
|
---|
[28a5ebd] | 143 | vt100_putuchar_t putuchar_fn, vt100_control_puts_t control_puts_fn,
|
---|
[1433ecda] | 144 | vt100_flush_t flush_fn)
|
---|
[7c014d1] | 145 | {
|
---|
[47c1437] | 146 | vt100_state_t *state = malloc(sizeof(vt100_state_t));
|
---|
[7c014d1] | 147 | if (state == NULL)
|
---|
| 148 | return NULL;
|
---|
[a35b458] | 149 |
|
---|
[28a5ebd] | 150 | state->putuchar = putuchar_fn;
|
---|
[7c014d1] | 151 | state->control_puts = control_puts_fn;
|
---|
[bbc6277] | 152 | state->flush = flush_fn;
|
---|
[a35b458] | 153 |
|
---|
[6d5e378] | 154 | state->cols = cols;
|
---|
| 155 | state->rows = rows;
|
---|
[a35b458] | 156 |
|
---|
[7c014d1] | 157 | state->cur_col = (sysarg_t) -1;
|
---|
| 158 | state->cur_row = (sysarg_t) -1;
|
---|
[a35b458] | 159 |
|
---|
[7c014d1] | 160 | state->cur_attrs.type = CHAR_ATTR_STYLE;
|
---|
| 161 | state->cur_attrs.val.style = STYLE_NORMAL;
|
---|
[a35b458] | 162 |
|
---|
[7c014d1] | 163 | /* Initialize graphic rendition attributes */
|
---|
| 164 | vt100_sgr(state, SGR_RESET);
|
---|
| 165 | vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
|
---|
| 166 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
| 167 | state->control_puts("\033[2J");
|
---|
[6d5e378] | 168 | state->control_puts("\033[?25l");
|
---|
[47c1437] | 169 |
|
---|
[7c014d1] | 170 | return state;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[6d5e378] | 173 | void vt100_state_destroy(vt100_state_t *state)
|
---|
| 174 | {
|
---|
| 175 | free(state);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void vt100_get_dimensions(vt100_state_t *state, sysarg_t *cols,
|
---|
| 179 | sysarg_t *rows)
|
---|
[7c014d1] | 180 | {
|
---|
[6d5e378] | 181 | *cols = state->cols;
|
---|
| 182 | *rows = state->rows;
|
---|
[7c014d1] | 183 | }
|
---|
| 184 |
|
---|
[b7fd2a0] | 185 | errno_t vt100_yield(vt100_state_t *state)
|
---|
[7c014d1] | 186 | {
|
---|
| 187 | return EOK;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[b7fd2a0] | 190 | errno_t vt100_claim(vt100_state_t *state)
|
---|
[7c014d1] | 191 | {
|
---|
| 192 | return EOK;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[6d5e378] | 195 | void vt100_goto(vt100_state_t *state, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 196 | {
|
---|
[6d5e378] | 197 | if ((col >= state->cols) || (row >= state->rows))
|
---|
| 198 | return;
|
---|
[a35b458] | 199 |
|
---|
[6d5e378] | 200 | if ((col != state->cur_col) || (row != state->cur_row)) {
|
---|
| 201 | vt100_set_pos(state, col, row);
|
---|
| 202 | state->cur_col = col;
|
---|
| 203 | state->cur_row = row;
|
---|
[7c014d1] | 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | void vt100_set_attr(vt100_state_t *state, char_attrs_t attrs)
|
---|
| 208 | {
|
---|
| 209 | if (!attrs_same(state->cur_attrs, attrs)) {
|
---|
| 210 | vt100_set_sgr(state, attrs);
|
---|
| 211 | state->cur_attrs = attrs;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | void vt100_cursor_visibility(vt100_state_t *state, bool visible)
|
---|
| 216 | {
|
---|
| 217 | if (visible)
|
---|
| 218 | state->control_puts("\033[?25h");
|
---|
| 219 | else
|
---|
| 220 | state->control_puts("\033[?25l");
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[28a5ebd] | 223 | void vt100_putuchar(vt100_state_t *state, char32_t ch)
|
---|
[6d5e378] | 224 | {
|
---|
[28a5ebd] | 225 | state->putuchar(ch == 0 ? ' ' : ch);
|
---|
[6d5e378] | 226 | state->cur_col++;
|
---|
[a35b458] | 227 |
|
---|
[6d5e378] | 228 | if (state->cur_col >= state->cols) {
|
---|
| 229 | state->cur_row += state->cur_col / state->cols;
|
---|
| 230 | state->cur_col %= state->cols;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[bbc6277] | 234 | void vt100_flush(vt100_state_t *state)
|
---|
| 235 | {
|
---|
| 236 | state->flush();
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[7c014d1] | 239 | /** @}
|
---|
| 240 | */
|
---|