1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * Copyright (c) 2006 Josef Cejka
|
---|
4 | * Copyright (c) 2006 Jakub Vana
|
---|
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 libc
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <as.h>
|
---|
38 | #include <libc.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <vfs/vfs_sess.h>
|
---|
43 | #include <io/console.h>
|
---|
44 | #include <ipc/console.h>
|
---|
45 |
|
---|
46 | console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
|
---|
47 | {
|
---|
48 | console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
|
---|
49 | if (!ctrl)
|
---|
50 | return NULL;
|
---|
51 |
|
---|
52 | ctrl->input_sess = vfs_fsession(ifile, INTERFACE_CONSOLE);
|
---|
53 | if (!ctrl->input_sess) {
|
---|
54 | free(ctrl);
|
---|
55 | return NULL;
|
---|
56 | }
|
---|
57 |
|
---|
58 | ctrl->output_sess = vfs_fsession(ofile, INTERFACE_CONSOLE);
|
---|
59 | if (!ctrl->output_sess) {
|
---|
60 | free(ctrl);
|
---|
61 | return NULL;
|
---|
62 | }
|
---|
63 |
|
---|
64 | ctrl->input = ifile;
|
---|
65 | ctrl->output = ofile;
|
---|
66 | ctrl->input_aid = 0;
|
---|
67 |
|
---|
68 | return ctrl;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void console_done(console_ctrl_t *ctrl)
|
---|
72 | {
|
---|
73 | free(ctrl);
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool console_kcon(void)
|
---|
77 | {
|
---|
78 | return __SYSCALL0(SYS_DEBUG_CONSOLE);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void console_flush(console_ctrl_t *ctrl)
|
---|
82 | {
|
---|
83 | fflush(ctrl->output);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void console_clear(console_ctrl_t *ctrl)
|
---|
87 | {
|
---|
88 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
89 | async_req_0_0(exch, CONSOLE_CLEAR);
|
---|
90 | async_exchange_end(exch);
|
---|
91 | }
|
---|
92 |
|
---|
93 | errno_t console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
|
---|
94 | {
|
---|
95 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
96 | errno_t rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
|
---|
97 | async_exchange_end(exch);
|
---|
98 |
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void console_set_style(console_ctrl_t *ctrl, uint8_t style)
|
---|
103 | {
|
---|
104 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
105 | async_req_1_0(exch, CONSOLE_SET_STYLE, style);
|
---|
106 | async_exchange_end(exch);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
|
---|
110 | uint8_t flags)
|
---|
111 | {
|
---|
112 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
113 | async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
|
---|
114 | async_exchange_end(exch);
|
---|
115 | }
|
---|
116 |
|
---|
117 | void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
|
---|
118 | uint32_t fgcolor)
|
---|
119 | {
|
---|
120 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
121 | async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
|
---|
122 | async_exchange_end(exch);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
|
---|
126 | {
|
---|
127 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
128 | async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
|
---|
129 | async_exchange_end(exch);
|
---|
130 | }
|
---|
131 |
|
---|
132 | errno_t console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
|
---|
133 | {
|
---|
134 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
135 | errno_t rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
|
---|
136 | async_exchange_end(exch);
|
---|
137 |
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | errno_t console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
|
---|
142 | {
|
---|
143 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
144 | errno_t rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
|
---|
145 | async_exchange_end(exch);
|
---|
146 |
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
|
---|
151 | {
|
---|
152 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
153 | async_req_2_0(exch, CONSOLE_SET_POS, col, row);
|
---|
154 | async_exchange_end(exch);
|
---|
155 | }
|
---|
156 |
|
---|
157 | static errno_t console_ev_decode(ipc_call_t *call, cons_event_t *event)
|
---|
158 | {
|
---|
159 | event->type = ipc_get_arg1(call);
|
---|
160 |
|
---|
161 | switch (event->type) {
|
---|
162 | case CEV_KEY:
|
---|
163 | event->ev.key.type = ipc_get_arg2(call);
|
---|
164 | event->ev.key.key = ipc_get_arg3(call);
|
---|
165 | event->ev.key.mods = ipc_get_arg4(call);
|
---|
166 | event->ev.key.c = ipc_get_arg5(call);
|
---|
167 | break;
|
---|
168 | case CEV_POS:
|
---|
169 | event->ev.pos.pos_id = ipc_get_arg2(call) >> 16;
|
---|
170 | event->ev.pos.type = ipc_get_arg2(call) & 0xffff;
|
---|
171 | event->ev.pos.btn_num = ipc_get_arg3(call);
|
---|
172 | event->ev.pos.hpos = ipc_get_arg4(call);
|
---|
173 | event->ev.pos.vpos = ipc_get_arg5(call);
|
---|
174 | break;
|
---|
175 | default:
|
---|
176 | return EIO;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
|
---|
183 | {
|
---|
184 | if (ctrl->input_aid == 0) {
|
---|
185 | ipc_call_t result;
|
---|
186 |
|
---|
187 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
188 | aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
|
---|
189 | async_exchange_end(exch);
|
---|
190 |
|
---|
191 | errno_t rc;
|
---|
192 | async_wait_for(aid, &rc);
|
---|
193 |
|
---|
194 | if (rc != EOK) {
|
---|
195 | errno = rc;
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 |
|
---|
199 | rc = console_ev_decode(&result, event);
|
---|
200 | if (rc != EOK) {
|
---|
201 | errno = rc;
|
---|
202 | return false;
|
---|
203 | }
|
---|
204 | } else {
|
---|
205 | errno_t retval;
|
---|
206 | async_wait_for(ctrl->input_aid, &retval);
|
---|
207 |
|
---|
208 | ctrl->input_aid = 0;
|
---|
209 |
|
---|
210 | if (retval != EOK) {
|
---|
211 | errno = retval;
|
---|
212 | return false;
|
---|
213 | }
|
---|
214 |
|
---|
215 | errno_t rc = console_ev_decode(&ctrl->input_call, event);
|
---|
216 | if (rc != EOK) {
|
---|
217 | errno = rc;
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
|
---|
226 | usec_t *timeout)
|
---|
227 | {
|
---|
228 | struct timespec t0;
|
---|
229 | getuptime(&t0);
|
---|
230 |
|
---|
231 | if (ctrl->input_aid == 0) {
|
---|
232 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
233 | ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
|
---|
234 | &ctrl->input_call);
|
---|
235 | async_exchange_end(exch);
|
---|
236 | }
|
---|
237 |
|
---|
238 | errno_t retval;
|
---|
239 | errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
|
---|
240 | if (rc != EOK) {
|
---|
241 | *timeout = 0;
|
---|
242 | errno = rc;
|
---|
243 | return false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | ctrl->input_aid = 0;
|
---|
247 |
|
---|
248 | if (retval != EOK) {
|
---|
249 | errno = retval;
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 |
|
---|
253 | rc = console_ev_decode(&ctrl->input_call, event);
|
---|
254 | if (rc != EOK) {
|
---|
255 | errno = rc;
|
---|
256 | return false;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /* Update timeout */
|
---|
260 | struct timespec t1;
|
---|
261 | getuptime(&t1);
|
---|
262 | *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
|
---|
263 |
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Create a shared buffer for fast rendering to the console.
|
---|
268 | *
|
---|
269 | * @param ctrl Console
|
---|
270 | * @param cols Number of columns
|
---|
271 | * @param rows Number of rows
|
---|
272 | * @param rbuf Place to store pointer to the shared buffer
|
---|
273 | * @return EOK on success or an error code
|
---|
274 | */
|
---|
275 | errno_t console_map(console_ctrl_t *ctrl, sysarg_t cols, sysarg_t rows,
|
---|
276 | charfield_t **rbuf)
|
---|
277 | {
|
---|
278 | async_exch_t *exch = NULL;
|
---|
279 | void *buf;
|
---|
280 | aid_t req;
|
---|
281 | ipc_call_t answer;
|
---|
282 | size_t asize;
|
---|
283 | errno_t rc;
|
---|
284 |
|
---|
285 | exch = async_exchange_begin(ctrl->output_sess);
|
---|
286 | req = async_send_2(exch, CONSOLE_MAP, cols, rows, &answer);
|
---|
287 | if (rc != EOK)
|
---|
288 | goto error;
|
---|
289 |
|
---|
290 | asize = PAGES2SIZE(SIZE2PAGES(cols * rows * sizeof(charfield_t)));
|
---|
291 |
|
---|
292 | rc = async_share_in_start_0_0(exch, asize, &buf);
|
---|
293 | if (rc != EOK) {
|
---|
294 | async_forget(req);
|
---|
295 | goto error;
|
---|
296 | }
|
---|
297 |
|
---|
298 | async_exchange_end(exch);
|
---|
299 | exch = NULL;
|
---|
300 |
|
---|
301 | async_wait_for(req, &rc);
|
---|
302 | if (rc != EOK)
|
---|
303 | goto error;
|
---|
304 |
|
---|
305 | *rbuf = (charfield_t *)buf;
|
---|
306 | return EOK;
|
---|
307 | error:
|
---|
308 | if (exch != NULL)
|
---|
309 | async_exchange_end(exch);
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** Unmap console shared buffer.
|
---|
314 | *
|
---|
315 | * @param ctrl Console
|
---|
316 | * @param buf Buffer
|
---|
317 | */
|
---|
318 | void console_unmap(console_ctrl_t *ctrl, charfield_t *buf)
|
---|
319 | {
|
---|
320 | as_area_destroy(buf);
|
---|
321 | }
|
---|
322 |
|
---|
323 | /** Update console rectangle from shared buffer.
|
---|
324 | *
|
---|
325 | * @param ctrl Console
|
---|
326 | * @param c0 Column coordinate of top-left corner (inclusive)
|
---|
327 | * @param r0 Row coordinate of top-left corner (inclusive)
|
---|
328 | * @param c1 Column coordinate of bottom-right corner (exclusive)
|
---|
329 | * @param r1 Row coordinate of bottom-right corner (exclusive)
|
---|
330 | *
|
---|
331 | * @return EOK on sucess or an error code
|
---|
332 | */
|
---|
333 | errno_t console_update(console_ctrl_t *ctrl, sysarg_t c0, sysarg_t r0,
|
---|
334 | sysarg_t c1, sysarg_t r1)
|
---|
335 | {
|
---|
336 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
337 | errno_t rc = async_req_4_0(exch, CONSOLE_UPDATE, c0, r0, c1, r1);
|
---|
338 | async_exchange_end(exch);
|
---|
339 |
|
---|
340 | return rc;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** @}
|
---|
344 | */
|
---|