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