[7c014d1] | 1 | /*
|
---|
[d3109ff] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[7c014d1] | 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 |
|
---|
[4122410] | 31 | /** @addtogroup output
|
---|
[6d5e378] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
[7c014d1] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
[6d5e378] | 39 | #include <io/chargrid.h>
|
---|
[d3109ff] | 40 | #include <vt/vt100.h>
|
---|
[6d5e378] | 41 | #include "../output.h"
|
---|
[7c014d1] | 42 | #include "serial.h"
|
---|
| 43 |
|
---|
[6d5e378] | 44 | #define SERIAL_COLS 80
|
---|
| 45 | #define SERIAL_ROWS 24
|
---|
| 46 |
|
---|
[d31c3ea] | 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 |
|
---|
[6d5e378] | 61 | /** Draw the character at the specified position.
|
---|
[7c014d1] | 62 | *
|
---|
| 63 | * @param state VT100 protocol state.
|
---|
[6d5e378] | 64 | * @param field Character field.
|
---|
| 65 | * @param col Horizontal screen position.
|
---|
| 66 | * @param row Vertical screen position.
|
---|
[7c014d1] | 67 | *
|
---|
| 68 | */
|
---|
[357d9dd] | 69 | static void draw_char(vt100_t *state, charfield_t *field,
|
---|
[6d5e378] | 70 | sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 71 | {
|
---|
[6d5e378] | 72 | vt100_goto(state, col, row);
|
---|
[7c014d1] | 73 | vt100_set_attr(state, field->attrs);
|
---|
[28a5ebd] | 74 | vt100_putuchar(state, field->ch);
|
---|
[7c014d1] | 75 | }
|
---|
| 76 |
|
---|
[b7fd2a0] | 77 | static errno_t serial_yield(outdev_t *dev)
|
---|
[7c014d1] | 78 | {
|
---|
[357d9dd] | 79 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[a35b458] | 80 |
|
---|
[7c014d1] | 81 | return vt100_yield(state);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[b7fd2a0] | 84 | static errno_t serial_claim(outdev_t *dev)
|
---|
[7c014d1] | 85 | {
|
---|
[357d9dd] | 86 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[a35b458] | 87 |
|
---|
[6d5e378] | 88 | return vt100_claim(state);
|
---|
[7c014d1] | 89 | }
|
---|
| 90 |
|
---|
[6d5e378] | 91 | static void serial_get_dimensions(outdev_t *dev, sysarg_t *cols,
|
---|
| 92 | sysarg_t *rows)
|
---|
[7c014d1] | 93 | {
|
---|
[357d9dd] | 94 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[a35b458] | 95 |
|
---|
[6d5e378] | 96 | vt100_get_dimensions(state, cols, rows);
|
---|
[7c014d1] | 97 | }
|
---|
| 98 |
|
---|
[6d5e378] | 99 | static console_caps_t serial_get_caps(outdev_t *dev)
|
---|
[7c014d1] | 100 | {
|
---|
[09f41d3] | 101 | return (CONSOLE_CAP_CURSORCTL | CONSOLE_CAP_STYLE |
|
---|
| 102 | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB);
|
---|
[7c014d1] | 103 | }
|
---|
| 104 |
|
---|
[6d5e378] | 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)
|
---|
[7c014d1] | 107 | {
|
---|
[357d9dd] | 108 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[a35b458] | 109 |
|
---|
[6d5e378] | 110 | vt100_goto(state, col, row);
|
---|
[7c014d1] | 111 | vt100_cursor_visibility(state, visible);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[6d5e378] | 114 | static void serial_char_update(outdev_t *dev, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 115 | {
|
---|
[357d9dd] | 116 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[6d5e378] | 117 | charfield_t *field =
|
---|
| 118 | chargrid_charfield_at(dev->backbuf, col, row);
|
---|
[a35b458] | 119 |
|
---|
[6d5e378] | 120 | draw_char(state, field, col, row);
|
---|
[7c014d1] | 121 | }
|
---|
| 122 |
|
---|
[bbc6277] | 123 | static void serial_flush(outdev_t *dev)
|
---|
| 124 | {
|
---|
[357d9dd] | 125 | vt100_t *state = (vt100_t *) dev->data;
|
---|
[bbc6277] | 126 |
|
---|
| 127 | vt100_flush(state);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[6d5e378] | 130 | static outdev_ops_t serial_ops = {
|
---|
[7c014d1] | 131 | .yield = serial_yield,
|
---|
| 132 | .claim = serial_claim,
|
---|
[6d5e378] | 133 | .get_dimensions = serial_get_dimensions,
|
---|
| 134 | .get_caps = serial_get_caps,
|
---|
| 135 | .cursor_update = serial_cursor_update,
|
---|
[bbc6277] | 136 | .char_update = serial_char_update,
|
---|
| 137 | .flush = serial_flush
|
---|
[7c014d1] | 138 | };
|
---|
| 139 |
|
---|
[d31c3ea] | 140 | errno_t serial_init(serial_putuchar_t putuchar_fn,
|
---|
| 141 | serial_control_puts_t control_puts_fn, serial_flush_t flush_fn)
|
---|
[7c014d1] | 142 | {
|
---|
[d3109ff] | 143 | char_attrs_t attrs;
|
---|
[d31c3ea] | 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);
|
---|
[357d9dd] | 151 | if (vt100 == NULL)
|
---|
[7c014d1] | 152 | return ENOMEM;
|
---|
[357d9dd] | 153 | vt100->enable_rgb = true;
|
---|
[d3109ff] | 154 |
|
---|
[357d9dd] | 155 | vt100_cursor_visibility(vt100, false);
|
---|
[d3109ff] | 156 | attrs.type = CHAR_ATTR_STYLE;
|
---|
| 157 | attrs.val.style = STYLE_NORMAL;
|
---|
[357d9dd] | 158 | vt100_set_attr(vt100, attrs);
|
---|
| 159 | vt100_cls(vt100);
|
---|
[a35b458] | 160 |
|
---|
[357d9dd] | 161 | outdev_t *dev = outdev_register(&serial_ops, vt100);
|
---|
[6d5e378] | 162 | if (dev == NULL) {
|
---|
[357d9dd] | 163 | vt100_destroy(vt100);
|
---|
[6d5e378] | 164 | return ENOMEM;
|
---|
| 165 | }
|
---|
[a35b458] | 166 |
|
---|
[7c014d1] | 167 | return EOK;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[d31c3ea] | 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 |
|
---|
[7c014d1] | 188 | /** @}
|
---|
| 189 | */
|
---|