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