source: mainline/uspace/srv/hid/output/proto/vt100.c@ b7fd2a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 <stddef.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <io/color.h>
38#include <types/common.h>
39#include "vt100.h"
40
41#define MAX_CONTROL 20
42
43typedef 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
54typedef 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
64static 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. */
76static 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
84static 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
93static 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
140vt100_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 vt100_flush_t flush_fn)
143{
144 vt100_state_t *state = malloc(sizeof(vt100_state_t));
145 if (state == NULL)
146 return NULL;
147
148 state->putchar = putchar_fn;
149 state->control_puts = control_puts_fn;
150 state->flush = flush_fn;
151
152 state->cols = cols;
153 state->rows = rows;
154
155 state->cur_col = (sysarg_t) -1;
156 state->cur_row = (sysarg_t) -1;
157
158 state->cur_attrs.type = CHAR_ATTR_STYLE;
159 state->cur_attrs.val.style = STYLE_NORMAL;
160
161 /* Initialize graphic rendition attributes */
162 vt100_sgr(state, SGR_RESET);
163 vt100_sgr(state, SGR_FGCOLOR + CI_BLACK);
164 vt100_sgr(state, SGR_BGCOLOR + CI_WHITE);
165 state->control_puts("\033[2J");
166 state->control_puts("\033[?25l");
167
168 return state;
169}
170
171void vt100_state_destroy(vt100_state_t *state)
172{
173 free(state);
174}
175
176void vt100_get_dimensions(vt100_state_t *state, sysarg_t *cols,
177 sysarg_t *rows)
178{
179 *cols = state->cols;
180 *rows = state->rows;
181}
182
183errno_t vt100_yield(vt100_state_t *state)
184{
185 return EOK;
186}
187
188errno_t vt100_claim(vt100_state_t *state)
189{
190 return EOK;
191}
192
193void vt100_goto(vt100_state_t *state, sysarg_t col, sysarg_t row)
194{
195 if ((col >= state->cols) || (row >= state->rows))
196 return;
197
198 if ((col != state->cur_col) || (row != state->cur_row)) {
199 vt100_set_pos(state, col, row);
200 state->cur_col = col;
201 state->cur_row = row;
202 }
203}
204
205void vt100_set_attr(vt100_state_t *state, char_attrs_t attrs)
206{
207 if (!attrs_same(state->cur_attrs, attrs)) {
208 vt100_set_sgr(state, attrs);
209 state->cur_attrs = attrs;
210 }
211}
212
213void vt100_cursor_visibility(vt100_state_t *state, bool visible)
214{
215 if (visible)
216 state->control_puts("\033[?25h");
217 else
218 state->control_puts("\033[?25l");
219}
220
221void vt100_putchar(vt100_state_t *state, wchar_t ch)
222{
223 state->putchar(ch == 0 ? ' ' : ch);
224 state->cur_col++;
225
226 if (state->cur_col >= state->cols) {
227 state->cur_row += state->cur_col / state->cols;
228 state->cur_col %= state->cols;
229 }
230}
231
232void vt100_flush(vt100_state_t *state)
233{
234 state->flush();
235}
236
237/** @}
238 */
Note: See TracBrowser for help on using the repository browser.