1 | /*
|
---|
2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
3 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
4 | * Copyright (c) 2008 Martin Decky
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup output
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/chargrid.h>
|
---|
40 | #include <vt/vt100.h>
|
---|
41 | #include "../output.h"
|
---|
42 | #include "serial.h"
|
---|
43 |
|
---|
44 | #define SERIAL_COLS 80
|
---|
45 | #define SERIAL_ROWS 24
|
---|
46 |
|
---|
47 | static serial_putuchar_t serial_putuchar_fn;
|
---|
48 | static serial_control_puts_t serial_control_puts_fn;
|
---|
49 | static serial_flush_t serial_flush_fn;
|
---|
50 |
|
---|
51 | static void serial_vt_putuchar(void *, char32_t);
|
---|
52 | static void serial_vt_control_puts(void *, const char *);
|
---|
53 | static void serial_vt_flush(void *);
|
---|
54 |
|
---|
55 | static vt100_cb_t serial_vt_cb = {
|
---|
56 | .putuchar = serial_vt_putuchar,
|
---|
57 | .control_puts = serial_vt_control_puts,
|
---|
58 | .flush = serial_vt_flush
|
---|
59 | };
|
---|
60 |
|
---|
61 | /** Draw the character at the specified position.
|
---|
62 | *
|
---|
63 | * @param state VT100 protocol state.
|
---|
64 | * @param field Character field.
|
---|
65 | * @param col Horizontal screen position.
|
---|
66 | * @param row Vertical screen position.
|
---|
67 | *
|
---|
68 | */
|
---|
69 | static void draw_char(vt100_t *state, charfield_t *field,
|
---|
70 | sysarg_t col, sysarg_t row)
|
---|
71 | {
|
---|
72 | vt100_goto(state, col, row);
|
---|
73 | vt100_set_attr(state, field->attrs);
|
---|
74 | vt100_putuchar(state, field->ch);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static errno_t serial_yield(outdev_t *dev)
|
---|
78 | {
|
---|
79 | vt100_t *state = (vt100_t *) dev->data;
|
---|
80 |
|
---|
81 | return vt100_yield(state);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static errno_t serial_claim(outdev_t *dev)
|
---|
85 | {
|
---|
86 | vt100_t *state = (vt100_t *) dev->data;
|
---|
87 |
|
---|
88 | return vt100_claim(state);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void serial_get_dimensions(outdev_t *dev, sysarg_t *cols,
|
---|
92 | sysarg_t *rows)
|
---|
93 | {
|
---|
94 | vt100_t *state = (vt100_t *) dev->data;
|
---|
95 |
|
---|
96 | vt100_get_dimensions(state, cols, rows);
|
---|
97 | }
|
---|
98 |
|
---|
99 | static console_caps_t serial_get_caps(outdev_t *dev)
|
---|
100 | {
|
---|
101 | return (CONSOLE_CAP_CURSORCTL | CONSOLE_CAP_STYLE |
|
---|
102 | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void serial_cursor_update(outdev_t *dev, sysarg_t prev_col,
|
---|
106 | sysarg_t prev_row, sysarg_t col, sysarg_t row, bool visible)
|
---|
107 | {
|
---|
108 | vt100_t *state = (vt100_t *) dev->data;
|
---|
109 |
|
---|
110 | vt100_goto(state, col, row);
|
---|
111 | vt100_cursor_visibility(state, visible);
|
---|
112 | }
|
---|
113 |
|
---|
114 | static void serial_char_update(outdev_t *dev, sysarg_t col, sysarg_t row)
|
---|
115 | {
|
---|
116 | vt100_t *state = (vt100_t *) dev->data;
|
---|
117 | charfield_t *field =
|
---|
118 | chargrid_charfield_at(dev->backbuf, col, row);
|
---|
119 |
|
---|
120 | draw_char(state, field, col, row);
|
---|
121 | }
|
---|
122 |
|
---|
123 | static void serial_flush(outdev_t *dev)
|
---|
124 | {
|
---|
125 | vt100_t *state = (vt100_t *) dev->data;
|
---|
126 |
|
---|
127 | vt100_flush(state);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static outdev_ops_t serial_ops = {
|
---|
131 | .yield = serial_yield,
|
---|
132 | .claim = serial_claim,
|
---|
133 | .get_dimensions = serial_get_dimensions,
|
---|
134 | .get_caps = serial_get_caps,
|
---|
135 | .cursor_update = serial_cursor_update,
|
---|
136 | .char_update = serial_char_update,
|
---|
137 | .flush = serial_flush
|
---|
138 | };
|
---|
139 |
|
---|
140 | errno_t serial_init(serial_putuchar_t putuchar_fn,
|
---|
141 | serial_control_puts_t control_puts_fn, serial_flush_t flush_fn)
|
---|
142 | {
|
---|
143 | char_attrs_t attrs;
|
---|
144 | vt100_t *vt100;
|
---|
145 |
|
---|
146 | serial_putuchar_fn = putuchar_fn;
|
---|
147 | serial_control_puts_fn = control_puts_fn;
|
---|
148 | serial_flush_fn = flush_fn;
|
---|
149 |
|
---|
150 | vt100 = vt100_create(NULL, SERIAL_COLS, SERIAL_ROWS, &serial_vt_cb);
|
---|
151 | if (vt100 == NULL)
|
---|
152 | return ENOMEM;
|
---|
153 | vt100->enable_rgb = true;
|
---|
154 |
|
---|
155 | vt100_cursor_visibility(vt100, false);
|
---|
156 | attrs.type = CHAR_ATTR_STYLE;
|
---|
157 | attrs.val.style = STYLE_NORMAL;
|
---|
158 | vt100_set_attr(vt100, attrs);
|
---|
159 | vt100_cls(vt100);
|
---|
160 |
|
---|
161 | outdev_t *dev = outdev_register(&serial_ops, vt100);
|
---|
162 | if (dev == NULL) {
|
---|
163 | vt100_destroy(vt100);
|
---|
164 | return ENOMEM;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return EOK;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void serial_vt_putuchar(void *arg, char32_t c)
|
---|
171 | {
|
---|
172 | (void)arg;
|
---|
173 | serial_putuchar_fn(c);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void serial_vt_control_puts(void *arg, const char *str)
|
---|
177 | {
|
---|
178 | (void)arg;
|
---|
179 | serial_control_puts_fn(str);
|
---|
180 | }
|
---|
181 |
|
---|
182 | static void serial_vt_flush(void *arg)
|
---|
183 | {
|
---|
184 | (void)arg;
|
---|
185 | serial_flush_fn();
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** @}
|
---|
189 | */
|
---|