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 | /** Draw the character at the specified position.
|
---|
48 | *
|
---|
49 | * @param state VT100 protocol state.
|
---|
50 | * @param field Character field.
|
---|
51 | * @param col Horizontal screen position.
|
---|
52 | * @param row Vertical screen position.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | static void draw_char(vt100_t *state, charfield_t *field,
|
---|
56 | sysarg_t col, sysarg_t row)
|
---|
57 | {
|
---|
58 | vt100_goto(state, col, row);
|
---|
59 | vt100_set_attr(state, field->attrs);
|
---|
60 | vt100_putuchar(state, field->ch);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static errno_t serial_yield(outdev_t *dev)
|
---|
64 | {
|
---|
65 | vt100_t *state = (vt100_t *) dev->data;
|
---|
66 |
|
---|
67 | return vt100_yield(state);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static errno_t serial_claim(outdev_t *dev)
|
---|
71 | {
|
---|
72 | vt100_t *state = (vt100_t *) dev->data;
|
---|
73 |
|
---|
74 | return vt100_claim(state);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void serial_get_dimensions(outdev_t *dev, sysarg_t *cols,
|
---|
78 | sysarg_t *rows)
|
---|
79 | {
|
---|
80 | vt100_t *state = (vt100_t *) dev->data;
|
---|
81 |
|
---|
82 | vt100_get_dimensions(state, cols, rows);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static console_caps_t serial_get_caps(outdev_t *dev)
|
---|
86 | {
|
---|
87 | return (CONSOLE_CAP_CURSORCTL | CONSOLE_CAP_STYLE |
|
---|
88 | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void serial_cursor_update(outdev_t *dev, sysarg_t prev_col,
|
---|
92 | sysarg_t prev_row, sysarg_t col, sysarg_t row, bool visible)
|
---|
93 | {
|
---|
94 | vt100_t *state = (vt100_t *) dev->data;
|
---|
95 |
|
---|
96 | vt100_goto(state, col, row);
|
---|
97 | vt100_cursor_visibility(state, visible);
|
---|
98 | }
|
---|
99 |
|
---|
100 | static void serial_char_update(outdev_t *dev, sysarg_t col, sysarg_t row)
|
---|
101 | {
|
---|
102 | vt100_t *state = (vt100_t *) dev->data;
|
---|
103 | charfield_t *field =
|
---|
104 | chargrid_charfield_at(dev->backbuf, col, row);
|
---|
105 |
|
---|
106 | draw_char(state, field, col, row);
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void serial_flush(outdev_t *dev)
|
---|
110 | {
|
---|
111 | vt100_t *state = (vt100_t *) dev->data;
|
---|
112 |
|
---|
113 | vt100_flush(state);
|
---|
114 | }
|
---|
115 |
|
---|
116 | static outdev_ops_t serial_ops = {
|
---|
117 | .yield = serial_yield,
|
---|
118 | .claim = serial_claim,
|
---|
119 | .get_dimensions = serial_get_dimensions,
|
---|
120 | .get_caps = serial_get_caps,
|
---|
121 | .cursor_update = serial_cursor_update,
|
---|
122 | .char_update = serial_char_update,
|
---|
123 | .flush = serial_flush
|
---|
124 | };
|
---|
125 |
|
---|
126 | errno_t serial_init(vt100_putuchar_t putuchar_fn,
|
---|
127 | vt100_control_puts_t control_puts_fn, vt100_flush_t flush_fn)
|
---|
128 | {
|
---|
129 | char_attrs_t attrs;
|
---|
130 | vt100_t *vt100 =
|
---|
131 | vt100_create(NULL, SERIAL_COLS, SERIAL_ROWS, putuchar_fn,
|
---|
132 | control_puts_fn, flush_fn);
|
---|
133 | if (vt100 == NULL)
|
---|
134 | return ENOMEM;
|
---|
135 | vt100->enable_rgb = true;
|
---|
136 |
|
---|
137 | vt100_cursor_visibility(vt100, false);
|
---|
138 | attrs.type = CHAR_ATTR_STYLE;
|
---|
139 | attrs.val.style = STYLE_NORMAL;
|
---|
140 | vt100_set_attr(vt100, attrs);
|
---|
141 | vt100_cls(vt100);
|
---|
142 |
|
---|
143 | outdev_t *dev = outdev_register(&serial_ops, vt100);
|
---|
144 | if (dev == NULL) {
|
---|
145 | vt100_destroy(vt100);
|
---|
146 | return ENOMEM;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return EOK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** @}
|
---|
153 | */
|
---|