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