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 |
|
---|
29 | /** @file
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include <inttypes.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stddef.h>
|
---|
36 | #include <malloc.h>
|
---|
37 | #include <io/color.h>
|
---|
38 | #include <sys/types.h>
|
---|
39 | #include "vt100.h"
|
---|
40 |
|
---|
41 | #define MAX_CONTROL 20
|
---|
42 |
|
---|
43 | typedef enum {
|
---|
44 | CI_BLACK = 0,
|
---|
45 | CI_RED = 1,
|
---|
46 | CI_GREEN = 2,
|
---|
47 | CI_BROWN = 3,
|
---|
48 | CI_BLUE = 4,
|
---|
49 | CI_MAGENTA = 5,
|
---|
50 | CI_CYAN = 6,
|
---|
51 | CI_WHITE = 7
|
---|
52 | } sgr_color_index_t;
|
---|
53 |
|
---|
54 | typedef enum {
|
---|
55 | SGR_RESET = 0,
|
---|
56 | SGR_BOLD = 1,
|
---|
57 | SGR_UNDERLINE = 4,
|
---|
58 | SGR_BLINK = 5,
|
---|
59 | SGR_REVERSE = 7,
|
---|
60 | SGR_FGCOLOR = 30,
|
---|
61 | SGR_BGCOLOR = 40
|
---|
62 | } sgr_command_t;
|
---|
63 |
|
---|
64 | static sgr_color_index_t color_map[] = {
|
---|
65 | [COLOR_BLACK] = CI_BLACK,
|
---|
66 | [COLOR_BLUE] = CI_RED,
|
---|
67 | [COLOR_GREEN] = CI_GREEN,
|
---|
68 | [COLOR_CYAN] = CI_CYAN,
|
---|
69 | [COLOR_RED] = CI_RED,
|
---|
70 | [COLOR_MAGENTA] = CI_MAGENTA,
|
---|
71 | [COLOR_YELLOW] = CI_BROWN,
|
---|
72 | [COLOR_WHITE] = CI_WHITE
|
---|
73 | };
|
---|
74 |
|
---|
75 | /** ECMA-48 Set Graphics Rendition. */
|
---|
76 | static void vt100_sgr(vt100_state_t *state, unsigned int mode)
|
---|
77 | {
|
---|
78 | char control[MAX_CONTROL];
|
---|
79 |
|
---|
80 | snprintf(control, MAX_CONTROL, "\033[%um", mode);
|
---|
81 | state->control_puts(control);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void vt100_set_pos(vt100_state_t *state, sysarg_t col, sysarg_t row)
|
---|
85 | {
|
---|
86 | char control[MAX_CONTROL];
|
---|
87 |
|
---|
88 | snprintf(control, MAX_CONTROL, "\033[%" PRIun ";%" PRIun "f",
|
---|
89 | row + 1, col + 1);
|
---|
90 | state->control_puts(control);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void vt100_set_sgr(vt100_state_t *state, char_attrs_t attrs)
|
---|
94 | {
|
---|
95 | switch (attrs.type) {
|
---|
96 | case CHAR_ATTR_STYLE:
|
---|
97 | switch (attrs.val.style) {
|
---|
98 | case STYLE_NORMAL:
|
---|
99 | vt100_sgr(state, SGR_RESET);
|
---|
100 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
101 | vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
|
---|
102 | break;
|
---|
103 | case STYLE_EMPHASIS:
|
---|
104 | vt100_sgr(state, SGR_RESET);
|
---|
105 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
106 | vt100_sgr(state, SGR_FGCOLOR + CI_RED);
|
---|
107 | vt100_sgr(state, SGR_BOLD);
|
---|
108 | break;
|
---|
109 | case STYLE_INVERTED:
|
---|
110 | vt100_sgr(state, SGR_RESET);
|
---|
111 | vt100_sgr(state, SGR_BGCOLOR + CI_BLACK);
|
---|
112 | vt100_sgr(state, SGR_FGCOLOR + CI_WHITE);
|
---|
113 | break;
|
---|
114 | case STYLE_SELECTED:
|
---|
115 | vt100_sgr(state, SGR_RESET);
|
---|
116 | vt100_sgr(state, SGR_BGCOLOR + CI_RED);
|
---|
117 | vt100_sgr(state, SGR_FGCOLOR + CI_WHITE);
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | break;
|
---|
121 | case CHAR_ATTR_INDEX:
|
---|
122 | vt100_sgr(state, SGR_RESET);
|
---|
123 | vt100_sgr(state, SGR_BGCOLOR + color_map[attrs.val.index.bgcolor & 7]);
|
---|
124 | vt100_sgr(state, SGR_FGCOLOR + color_map[attrs.val.index.fgcolor & 7]);
|
---|
125 |
|
---|
126 | if (attrs.val.index.attr & CATTR_BRIGHT)
|
---|
127 | vt100_sgr(state, SGR_BOLD);
|
---|
128 |
|
---|
129 | break;
|
---|
130 | case CHAR_ATTR_RGB:
|
---|
131 | vt100_sgr(state, SGR_RESET);
|
---|
132 |
|
---|
133 | if (attrs.val.rgb.bgcolor <= attrs.val.rgb.fgcolor)
|
---|
134 | vt100_sgr(state, SGR_REVERSE);
|
---|
135 |
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | vt100_state_t *vt100_state_create(sysarg_t cols, sysarg_t rows,
|
---|
141 | vt100_putchar_t putchar_fn, vt100_control_puts_t control_puts_fn)
|
---|
142 | {
|
---|
143 | vt100_state_t *state = malloc(sizeof(vt100_state_t));
|
---|
144 | if (state == NULL)
|
---|
145 | return NULL;
|
---|
146 |
|
---|
147 | state->putchar = putchar_fn;
|
---|
148 | state->control_puts = control_puts_fn;
|
---|
149 |
|
---|
150 | state->cols = cols;
|
---|
151 | state->rows = rows;
|
---|
152 |
|
---|
153 | state->cur_col = (sysarg_t) -1;
|
---|
154 | state->cur_row = (sysarg_t) -1;
|
---|
155 |
|
---|
156 | state->cur_attrs.type = CHAR_ATTR_STYLE;
|
---|
157 | state->cur_attrs.val.style = STYLE_NORMAL;
|
---|
158 |
|
---|
159 | /* Initialize graphic rendition attributes */
|
---|
160 | vt100_sgr(state, SGR_RESET);
|
---|
161 | vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
|
---|
162 | vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
|
---|
163 | state->control_puts("\033[2J");
|
---|
164 | state->control_puts("\033[?25l");
|
---|
165 |
|
---|
166 | return state;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void vt100_state_destroy(vt100_state_t *state)
|
---|
170 | {
|
---|
171 | free(state);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void vt100_get_dimensions(vt100_state_t *state, sysarg_t *cols,
|
---|
175 | sysarg_t *rows)
|
---|
176 | {
|
---|
177 | *cols = state->cols;
|
---|
178 | *rows = state->rows;
|
---|
179 | }
|
---|
180 |
|
---|
181 | int vt100_yield(vt100_state_t *state)
|
---|
182 | {
|
---|
183 | return EOK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | int vt100_claim(vt100_state_t *state)
|
---|
187 | {
|
---|
188 | return EOK;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void vt100_goto(vt100_state_t *state, sysarg_t col, sysarg_t row)
|
---|
192 | {
|
---|
193 | if ((col >= state->cols) || (row >= state->rows))
|
---|
194 | return;
|
---|
195 |
|
---|
196 | if ((col != state->cur_col) || (row != state->cur_row)) {
|
---|
197 | vt100_set_pos(state, col, row);
|
---|
198 | state->cur_col = col;
|
---|
199 | state->cur_row = row;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | void vt100_set_attr(vt100_state_t *state, char_attrs_t attrs)
|
---|
204 | {
|
---|
205 | if (!attrs_same(state->cur_attrs, attrs)) {
|
---|
206 | vt100_set_sgr(state, attrs);
|
---|
207 | state->cur_attrs = attrs;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | void vt100_cursor_visibility(vt100_state_t *state, bool visible)
|
---|
212 | {
|
---|
213 | if (visible)
|
---|
214 | state->control_puts("\033[?25h");
|
---|
215 | else
|
---|
216 | state->control_puts("\033[?25l");
|
---|
217 | }
|
---|
218 |
|
---|
219 | void vt100_putchar(vt100_state_t *state, wchar_t ch)
|
---|
220 | {
|
---|
221 | state->putchar(ch == 0 ? ' ' : ch);
|
---|
222 | state->cur_col++;
|
---|
223 |
|
---|
224 | if (state->cur_col >= state->cols) {
|
---|
225 | state->cur_row += state->cur_col / state->cols;
|
---|
226 | state->cur_col %= state->cols;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** @}
|
---|
231 | */
|
---|