[b27a97bb] | 1 | /*
|
---|
[69c376b5] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[df4ed85] | 3 | * Copyright (c) 2006 Josef Cejka
|
---|
| 4 | * Copyright (c) 2006 Jakub Vana
|
---|
[b27a97bb] | 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 |
|
---|
[a635535] | 31 | /** @addtogroup libconsole
|
---|
[b2951e2] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[68a552f] | 37 | #include <as.h>
|
---|
[69c376b5] | 38 | #include <dbgcon.h>
|
---|
[afa6e74] | 39 | #include <libc.h>
|
---|
[b917098] | 40 | #include <async.h>
|
---|
[79ae36dd] | 41 | #include <errno.h>
|
---|
[38d150e] | 42 | #include <stdlib.h>
|
---|
[b48e680f] | 43 | #include <str.h>
|
---|
[79ae36dd] | 44 | #include <vfs/vfs_sess.h>
|
---|
[2595dab] | 45 | #include <io/console.h>
|
---|
| 46 | #include <ipc/console.h>
|
---|
[afa6e74] | 47 |
|
---|
[79ae36dd] | 48 | console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
|
---|
[afa6e74] | 49 | {
|
---|
[79ae36dd] | 50 | console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
|
---|
| 51 | if (!ctrl)
|
---|
| 52 | return NULL;
|
---|
[a35b458] | 53 |
|
---|
[6afc9d7] | 54 | ctrl->input_sess = vfs_fsession(ifile, INTERFACE_CONSOLE);
|
---|
[79ae36dd] | 55 | if (!ctrl->input_sess) {
|
---|
| 56 | free(ctrl);
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
[a35b458] | 59 |
|
---|
[6afc9d7] | 60 | ctrl->output_sess = vfs_fsession(ofile, INTERFACE_CONSOLE);
|
---|
[79ae36dd] | 61 | if (!ctrl->output_sess) {
|
---|
| 62 | free(ctrl);
|
---|
| 63 | return NULL;
|
---|
| 64 | }
|
---|
[a35b458] | 65 |
|
---|
[79ae36dd] | 66 | ctrl->input = ifile;
|
---|
| 67 | ctrl->output = ofile;
|
---|
| 68 | ctrl->input_aid = 0;
|
---|
[a35b458] | 69 |
|
---|
[79ae36dd] | 70 | return ctrl;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void console_done(console_ctrl_t *ctrl)
|
---|
| 74 | {
|
---|
| 75 | free(ctrl);
|
---|
[2595dab] | 76 | }
|
---|
| 77 |
|
---|
[79ae36dd] | 78 | bool console_kcon(void)
|
---|
[2595dab] | 79 | {
|
---|
[69c376b5] | 80 | return dbgcon_enable();
|
---|
[79ae36dd] | 81 | }
|
---|
| 82 |
|
---|
| 83 | void console_flush(console_ctrl_t *ctrl)
|
---|
| 84 | {
|
---|
| 85 | fflush(ctrl->output);
|
---|
[2595dab] | 86 | }
|
---|
| 87 |
|
---|
[79ae36dd] | 88 | void console_clear(console_ctrl_t *ctrl)
|
---|
[2595dab] | 89 | {
|
---|
[79ae36dd] | 90 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 91 | async_req_0_0(exch, CONSOLE_CLEAR);
|
---|
[79ae36dd] | 92 | async_exchange_end(exch);
|
---|
[2595dab] | 93 | }
|
---|
| 94 |
|
---|
[b7fd2a0] | 95 | errno_t console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
|
---|
[79ae36dd] | 96 | {
|
---|
| 97 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[b7fd2a0] | 98 | errno_t rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
|
---|
[79ae36dd] | 99 | async_exchange_end(exch);
|
---|
[a35b458] | 100 |
|
---|
[79ae36dd] | 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);
|
---|
[7c014d1] | 107 | async_req_1_0(exch, CONSOLE_SET_STYLE, style);
|
---|
[79ae36dd] | 108 | async_exchange_end(exch);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[7c014d1] | 111 | void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
|
---|
[9f1362d4] | 112 | uint8_t flags)
|
---|
[2595dab] | 113 | {
|
---|
[79ae36dd] | 114 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 115 | async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
|
---|
[79ae36dd] | 116 | async_exchange_end(exch);
|
---|
[d2cc7e1] | 117 | }
|
---|
| 118 |
|
---|
[7c014d1] | 119 | void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
|
---|
| 120 | uint32_t fgcolor)
|
---|
[0b6d70d] | 121 | {
|
---|
[79ae36dd] | 122 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 123 | async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
|
---|
[79ae36dd] | 124 | async_exchange_end(exch);
|
---|
[0b6d70d] | 125 | }
|
---|
| 126 |
|
---|
[79ae36dd] | 127 | void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
|
---|
[1c03c17] | 128 | {
|
---|
[79ae36dd] | 129 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[5d94b16c] | 130 | async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
|
---|
[79ae36dd] | 131 | async_exchange_end(exch);
|
---|
[2595dab] | 132 | }
|
---|
| 133 |
|
---|
[b48e680f] | 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 |
|
---|
[b7fd2a0] | 160 | errno_t console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
|
---|
[50cfa6c] | 161 | {
|
---|
[79ae36dd] | 162 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[b7fd2a0] | 163 | errno_t rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
|
---|
[79ae36dd] | 164 | async_exchange_end(exch);
|
---|
[a35b458] | 165 |
|
---|
[79ae36dd] | 166 | return rc;
|
---|
[50cfa6c] | 167 | }
|
---|
| 168 |
|
---|
[b7fd2a0] | 169 | errno_t console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
|
---|
[2595dab] | 170 | {
|
---|
[79ae36dd] | 171 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[b7fd2a0] | 172 | errno_t rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
|
---|
[79ae36dd] | 173 | async_exchange_end(exch);
|
---|
[a35b458] | 174 |
|
---|
[79ae36dd] | 175 | return rc;
|
---|
[2595dab] | 176 | }
|
---|
| 177 |
|
---|
[79ae36dd] | 178 | void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
|
---|
[19528516] | 179 | {
|
---|
[79ae36dd] | 180 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[5d94b16c] | 181 | async_req_2_0(exch, CONSOLE_SET_POS, col, row);
|
---|
[79ae36dd] | 182 | async_exchange_end(exch);
|
---|
[19528516] | 183 | }
|
---|
| 184 |
|
---|
[b7fd2a0] | 185 | static errno_t console_ev_decode(ipc_call_t *call, cons_event_t *event)
|
---|
[902f0906] | 186 | {
|
---|
[fafb8e5] | 187 | event->type = ipc_get_arg1(call);
|
---|
[902f0906] | 188 |
|
---|
| 189 | switch (event->type) {
|
---|
| 190 | case CEV_KEY:
|
---|
[fafb8e5] | 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);
|
---|
[899bdfd] | 195 | return EOK;
|
---|
[902f0906] | 196 | case CEV_POS:
|
---|
[fafb8e5] | 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);
|
---|
[899bdfd] | 202 | return EOK;
|
---|
| 203 | case CEV_RESIZE:
|
---|
| 204 | return EOK;
|
---|
[902f0906] | 205 | }
|
---|
| 206 |
|
---|
[899bdfd] | 207 | return EIO;
|
---|
[902f0906] | 208 | }
|
---|
| 209 |
|
---|
[87822ce] | 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)
|
---|
[2595dab] | 217 | {
|
---|
[79ae36dd] | 218 | if (ctrl->input_aid == 0) {
|
---|
[902f0906] | 219 | ipc_call_t result;
|
---|
[a35b458] | 220 |
|
---|
[79ae36dd] | 221 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
[902f0906] | 222 | aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
|
---|
[79ae36dd] | 223 | async_exchange_end(exch);
|
---|
[a35b458] | 224 |
|
---|
[b7fd2a0] | 225 | errno_t rc;
|
---|
[902f0906] | 226 | async_wait_for(aid, &rc);
|
---|
[a35b458] | 227 |
|
---|
[87822ce] | 228 | if (rc != EOK)
|
---|
| 229 | return EIO;
|
---|
[a35b458] | 230 |
|
---|
[902f0906] | 231 | rc = console_ev_decode(&result, event);
|
---|
[87822ce] | 232 | if (rc != EOK)
|
---|
| 233 | return EIO;
|
---|
[79ae36dd] | 234 | } else {
|
---|
[b7fd2a0] | 235 | errno_t retval;
|
---|
[79ae36dd] | 236 | async_wait_for(ctrl->input_aid, &retval);
|
---|
[a35b458] | 237 |
|
---|
[79ae36dd] | 238 | ctrl->input_aid = 0;
|
---|
[a35b458] | 239 |
|
---|
[87822ce] | 240 | if (retval != EOK)
|
---|
| 241 | return EIO;
|
---|
[a35b458] | 242 |
|
---|
[b7fd2a0] | 243 | errno_t rc = console_ev_decode(&ctrl->input_call, event);
|
---|
[87822ce] | 244 | if (rc != EOK)
|
---|
| 245 | return EIO;
|
---|
[79ae36dd] | 246 | }
|
---|
[a35b458] | 247 |
|
---|
[87822ce] | 248 | return EOK;
|
---|
[2595dab] | 249 | }
|
---|
| 250 |
|
---|
[87822ce] | 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,
|
---|
[bd41ac52] | 262 | usec_t *timeout)
|
---|
[2595dab] | 263 | {
|
---|
[bd41ac52] | 264 | struct timespec t0;
|
---|
| 265 | getuptime(&t0);
|
---|
[a35b458] | 266 |
|
---|
[79ae36dd] | 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 | }
|
---|
[a35b458] | 273 |
|
---|
[b7fd2a0] | 274 | errno_t retval;
|
---|
| 275 | errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
|
---|
[79ae36dd] | 276 | if (rc != EOK) {
|
---|
[87822ce] | 277 | if (rc == ENOMEM)
|
---|
| 278 | return ENOMEM;
|
---|
[79ae36dd] | 279 | *timeout = 0;
|
---|
[87822ce] | 280 | return ETIMEOUT;
|
---|
[79ae36dd] | 281 | }
|
---|
[a35b458] | 282 |
|
---|
[79ae36dd] | 283 | ctrl->input_aid = 0;
|
---|
[a35b458] | 284 |
|
---|
[87822ce] | 285 | if (retval != EOK)
|
---|
| 286 | return EIO;
|
---|
[a35b458] | 287 |
|
---|
[902f0906] | 288 | rc = console_ev_decode(&ctrl->input_call, event);
|
---|
[87822ce] | 289 | if (rc != EOK)
|
---|
| 290 | return EIO;
|
---|
[a35b458] | 291 |
|
---|
[79ae36dd] | 292 | /* Update timeout */
|
---|
[bd41ac52] | 293 | struct timespec t1;
|
---|
| 294 | getuptime(&t1);
|
---|
| 295 | *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
|
---|
[a35b458] | 296 |
|
---|
[87822ce] | 297 | return EOK;
|
---|
[1c03c17] | 298 | }
|
---|
| 299 |
|
---|
[68a552f] | 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 | {
|
---|
[d6c4d40] | 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 |
|
---|
[68a552f] | 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 |
|
---|
[a46da63] | 378 | /** @}
|
---|
[b2951e2] | 379 | */
|
---|