[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 |
|
---|
[07b7c48] | 182 | bool console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
|
---|
[2595dab] | 183 | {
|
---|
[79ae36dd] | 184 | if (ctrl->input_aid == 0) {
|
---|
[902f0906] | 185 | ipc_call_t result;
|
---|
[a35b458] | 186 |
|
---|
[79ae36dd] | 187 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
[902f0906] | 188 | aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
|
---|
[79ae36dd] | 189 | async_exchange_end(exch);
|
---|
[a35b458] | 190 |
|
---|
[b7fd2a0] | 191 | errno_t rc;
|
---|
[902f0906] | 192 | async_wait_for(aid, &rc);
|
---|
[a35b458] | 193 |
|
---|
[79ae36dd] | 194 | if (rc != EOK) {
|
---|
| 195 | errno = rc;
|
---|
| 196 | return false;
|
---|
| 197 | }
|
---|
[a35b458] | 198 |
|
---|
[902f0906] | 199 | rc = console_ev_decode(&result, event);
|
---|
| 200 | if (rc != EOK) {
|
---|
| 201 | errno = rc;
|
---|
| 202 | return false;
|
---|
| 203 | }
|
---|
[79ae36dd] | 204 | } else {
|
---|
[b7fd2a0] | 205 | errno_t retval;
|
---|
[79ae36dd] | 206 | async_wait_for(ctrl->input_aid, &retval);
|
---|
[a35b458] | 207 |
|
---|
[79ae36dd] | 208 | ctrl->input_aid = 0;
|
---|
[a35b458] | 209 |
|
---|
[79ae36dd] | 210 | if (retval != EOK) {
|
---|
[25a179e] | 211 | errno = retval;
|
---|
[79ae36dd] | 212 | return false;
|
---|
| 213 | }
|
---|
[a35b458] | 214 |
|
---|
[b7fd2a0] | 215 | errno_t rc = console_ev_decode(&ctrl->input_call, event);
|
---|
[902f0906] | 216 | if (rc != EOK) {
|
---|
| 217 | errno = rc;
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
[79ae36dd] | 220 | }
|
---|
[a35b458] | 221 |
|
---|
[79ae36dd] | 222 | return true;
|
---|
[2595dab] | 223 | }
|
---|
| 224 |
|
---|
[07b7c48] | 225 | bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
|
---|
[bd41ac52] | 226 | usec_t *timeout)
|
---|
[2595dab] | 227 | {
|
---|
[bd41ac52] | 228 | struct timespec t0;
|
---|
| 229 | getuptime(&t0);
|
---|
[a35b458] | 230 |
|
---|
[79ae36dd] | 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 | }
|
---|
[a35b458] | 237 |
|
---|
[b7fd2a0] | 238 | errno_t retval;
|
---|
| 239 | errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
|
---|
[79ae36dd] | 240 | if (rc != EOK) {
|
---|
| 241 | *timeout = 0;
|
---|
| 242 | errno = rc;
|
---|
| 243 | return false;
|
---|
| 244 | }
|
---|
[a35b458] | 245 |
|
---|
[79ae36dd] | 246 | ctrl->input_aid = 0;
|
---|
[a35b458] | 247 |
|
---|
[79ae36dd] | 248 | if (retval != EOK) {
|
---|
[25a179e] | 249 | errno = retval;
|
---|
[2595dab] | 250 | return false;
|
---|
[79ae36dd] | 251 | }
|
---|
[a35b458] | 252 |
|
---|
[902f0906] | 253 | rc = console_ev_decode(&ctrl->input_call, event);
|
---|
| 254 | if (rc != EOK) {
|
---|
| 255 | errno = rc;
|
---|
| 256 | return false;
|
---|
| 257 | }
|
---|
[a35b458] | 258 |
|
---|
[79ae36dd] | 259 | /* Update timeout */
|
---|
[bd41ac52] | 260 | struct timespec t1;
|
---|
| 261 | getuptime(&t1);
|
---|
| 262 | *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
|
---|
[a35b458] | 263 |
|
---|
[2595dab] | 264 | return true;
|
---|
[1c03c17] | 265 | }
|
---|
| 266 |
|
---|
[68a552f] | 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 |
|
---|
[a46da63] | 343 | /** @}
|
---|
[b2951e2] | 344 | */
|
---|