Changeset d3109ff in mainline for uspace/lib/vt/src/vt100.c
- Timestamp:
- 2024-09-24T17:59:36Z (8 months ago)
- Branches:
- master
- Children:
- 6a753a9c
- Parents:
- 3fcea34
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/vt/src/vt100.c
r3fcea34 rd3109ff 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * Copyright (c) 2011 Martin Decky 4 4 * All rights reserved. … … 28 28 */ 29 29 30 /** @addtogroup output30 /** @addtogroup libvt 31 31 * @{ 32 32 */ 33 33 34 #include <inttypes.h>35 34 #include <errno.h> 36 #include < stddef.h>35 #include <io/color.h> 37 36 #include <stdio.h> 38 37 #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 40 sgr_color_index_t color_map[] = { 76 41 [COLOR_BLACK] = CI_BLACK, 77 42 [COLOR_BLUE] = CI_BLUE, … … 84 49 }; 85 50 51 void vt100_cls(vt100_state_t *state) 52 { 53 state->control_puts(state->arg, "\033[2J"); 54 } 55 86 56 /** ECMA-48 Set Graphics Rendition. */ 87 57 static void vt100_sgr(vt100_state_t *state, unsigned int mode) … … 90 60 91 61 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. */ 66 static 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 76 void vt100_set_pos(vt100_state_t *state, sysarg_t col, sysarg_t row) 96 77 { 97 78 char control[MAX_CONTROL]; … … 99 80 snprintf(control, MAX_CONTROL, "\033[%" PRIun ";%" PRIun "f", 100 81 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 85 void vt100_set_sgr(vt100_state_t *state, char_attrs_t attrs) 86 { 87 unsigned color; 88 106 89 switch (attrs.type) { 107 90 case CHAR_ATTR_STYLE: … … 140 123 break; 141 124 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 } 147 145 break; 148 146 } 149 147 } 150 148 151 vt100_state_t *vt100_state_create( sysarg_t cols, sysarg_t rows,149 vt100_state_t *vt100_state_create(void *arg, sysarg_t cols, sysarg_t rows, 152 150 vt100_putuchar_t putuchar_fn, vt100_control_puts_t control_puts_fn, 153 151 vt100_flush_t flush_fn) … … 157 155 return NULL; 158 156 157 state->arg = arg; 159 158 state->putuchar = putuchar_fn; 160 159 state->control_puts = control_puts_fn; … … 169 168 state->cur_attrs.type = CHAR_ATTR_STYLE; 170 169 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");178 170 179 171 return state; … … 225 217 { 226 218 if (visible) 227 state->control_puts( "\033[?25h");219 state->control_puts(state->arg, "\033[?25h"); 228 220 else 229 state->control_puts( "\033[?25l");221 state->control_puts(state->arg, "\033[?25l"); 230 222 } 231 223 232 224 void vt100_putuchar(vt100_state_t *state, char32_t ch) 233 225 { 234 state->putuchar( ch == 0 ? ' ' : ch);226 state->putuchar(state->arg, ch == 0 ? ' ' : ch); 235 227 state->cur_col++; 236 228 … … 243 235 void vt100_flush(vt100_state_t *state) 244 236 { 245 state->flush( );237 state->flush(state->arg); 246 238 } 247 239
Note:
See TracChangeset
for help on using the changeset viewer.