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