[b27a97bb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
| 3 | * Copyright (c) 2006 Jakub Vana
|
---|
[2595dab] | 4 | * Copyright (c) 2008 Jiri Svoboda
|
---|
[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 |
|
---|
[afa6e74] | 37 | #include <libc.h>
|
---|
[b917098] | 38 | #include <async.h>
|
---|
[79ae36dd] | 39 | #include <errno.h>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <malloc.h>
|
---|
| 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;
|
---|
| 51 |
|
---|
| 52 | ctrl->input_sess = fsession(EXCHANGE_SERIALIZE, ifile);
|
---|
| 53 | if (!ctrl->input_sess) {
|
---|
| 54 | free(ctrl);
|
---|
| 55 | return NULL;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | ctrl->output_sess = fsession(EXCHANGE_SERIALIZE, ofile);
|
---|
| 59 | if (!ctrl->output_sess) {
|
---|
| 60 | free(ctrl);
|
---|
| 61 | return NULL;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | ctrl->input = ifile;
|
---|
| 65 | ctrl->output = ofile;
|
---|
| 66 | ctrl->input_aid = 0;
|
---|
| 67 |
|
---|
| 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 | {
|
---|
[79ae36dd] | 78 | return __SYSCALL0(SYS_DEBUG_ACTIVATE_CONSOLE);
|
---|
| 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);
|
---|
| 89 | async_msg_0(exch, CONSOLE_CLEAR);
|
---|
| 90 | async_exchange_end(exch);
|
---|
[2595dab] | 91 | }
|
---|
| 92 |
|
---|
[79ae36dd] | 93 | int console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
|
---|
| 94 | {
|
---|
| 95 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 96 | int rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
|
---|
| 97 | async_exchange_end(exch);
|
---|
| 98 |
|
---|
| 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);
|
---|
| 105 | async_msg_1(exch, CONSOLE_SET_STYLE, style);
|
---|
| 106 | async_exchange_end(exch);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void console_set_color(console_ctrl_t *ctrl, uint8_t fg_color, uint8_t bg_color,
|
---|
[9f1362d4] | 110 | uint8_t flags)
|
---|
[2595dab] | 111 | {
|
---|
[79ae36dd] | 112 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 113 | async_msg_3(exch, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
|
---|
| 114 | async_exchange_end(exch);
|
---|
[d2cc7e1] | 115 | }
|
---|
| 116 |
|
---|
[79ae36dd] | 117 | void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t fg_color,
|
---|
| 118 | uint32_t bg_color)
|
---|
[0b6d70d] | 119 | {
|
---|
[79ae36dd] | 120 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 121 | async_msg_2(exch, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
|
---|
| 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);
|
---|
| 128 | async_msg_1(exch, CONSOLE_CURSOR_VISIBILITY, (show != false));
|
---|
| 129 | async_exchange_end(exch);
|
---|
[2595dab] | 130 | }
|
---|
| 131 |
|
---|
[79ae36dd] | 132 | int 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);
|
---|
| 135 | int rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
|
---|
| 136 | async_exchange_end(exch);
|
---|
| 137 |
|
---|
| 138 | return rc;
|
---|
[50cfa6c] | 139 | }
|
---|
| 140 |
|
---|
[79ae36dd] | 141 | int 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);
|
---|
| 144 | int rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
|
---|
| 145 | async_exchange_end(exch);
|
---|
| 146 |
|
---|
| 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);
|
---|
| 153 | async_msg_2(exch, CONSOLE_GOTO, col, row);
|
---|
| 154 | async_exchange_end(exch);
|
---|
[19528516] | 155 | }
|
---|
| 156 |
|
---|
[79ae36dd] | 157 | bool console_get_kbd_event(console_ctrl_t *ctrl, kbd_event_t *event)
|
---|
[2595dab] | 158 | {
|
---|
[79ae36dd] | 159 | if (ctrl->input_aid == 0) {
|
---|
| 160 | sysarg_t type;
|
---|
| 161 | sysarg_t key;
|
---|
| 162 | sysarg_t mods;
|
---|
| 163 | sysarg_t c;
|
---|
| 164 |
|
---|
| 165 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
| 166 | int rc = async_req_0_4(exch, CONSOLE_GET_EVENT, &type, &key, &mods, &c);
|
---|
| 167 | async_exchange_end(exch);
|
---|
| 168 |
|
---|
| 169 | if (rc != EOK) {
|
---|
| 170 | errno = rc;
|
---|
| 171 | return false;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | event->type = type;
|
---|
| 175 | event->key = key;
|
---|
| 176 | event->mods = mods;
|
---|
| 177 | event->c = c;
|
---|
| 178 | } else {
|
---|
| 179 | sysarg_t retval;
|
---|
| 180 | async_wait_for(ctrl->input_aid, &retval);
|
---|
| 181 |
|
---|
| 182 | ctrl->input_aid = 0;
|
---|
| 183 |
|
---|
| 184 | if (retval != EOK) {
|
---|
| 185 | errno = (int) retval;
|
---|
| 186 | return false;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | event->type = IPC_GET_ARG1(ctrl->input_call);
|
---|
| 190 | event->key = IPC_GET_ARG2(ctrl->input_call);
|
---|
| 191 | event->mods = IPC_GET_ARG3(ctrl->input_call);
|
---|
| 192 | event->c = IPC_GET_ARG4(ctrl->input_call);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return true;
|
---|
[2595dab] | 196 | }
|
---|
| 197 |
|
---|
[79ae36dd] | 198 | bool console_get_kbd_event_timeout(console_ctrl_t *ctrl, kbd_event_t *event,
|
---|
| 199 | suseconds_t *timeout)
|
---|
[2595dab] | 200 | {
|
---|
[79ae36dd] | 201 | struct timeval t0;
|
---|
| 202 | gettimeofday(&t0, NULL);
|
---|
| 203 |
|
---|
| 204 | if (ctrl->input_aid == 0) {
|
---|
| 205 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
| 206 | ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
|
---|
| 207 | &ctrl->input_call);
|
---|
| 208 | async_exchange_end(exch);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | sysarg_t retval;
|
---|
| 212 | int rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
|
---|
| 213 | if (rc != EOK) {
|
---|
| 214 | *timeout = 0;
|
---|
| 215 | errno = rc;
|
---|
| 216 | return false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | ctrl->input_aid = 0;
|
---|
[2595dab] | 220 |
|
---|
[79ae36dd] | 221 | if (retval != EOK) {
|
---|
| 222 | errno = (int) retval;
|
---|
[2595dab] | 223 | return false;
|
---|
[79ae36dd] | 224 | }
|
---|
| 225 |
|
---|
| 226 | event->type = IPC_GET_ARG1(ctrl->input_call);
|
---|
| 227 | event->key = IPC_GET_ARG2(ctrl->input_call);
|
---|
| 228 | event->mods = IPC_GET_ARG3(ctrl->input_call);
|
---|
| 229 | event->c = IPC_GET_ARG4(ctrl->input_call);
|
---|
[2595dab] | 230 |
|
---|
[79ae36dd] | 231 | /* Update timeout */
|
---|
| 232 | struct timeval t1;
|
---|
| 233 | gettimeofday(&t1, NULL);
|
---|
| 234 | *timeout -= tv_sub(&t1, &t0);
|
---|
[2595dab] | 235 |
|
---|
| 236 | return true;
|
---|
[1c03c17] | 237 | }
|
---|
| 238 |
|
---|
[a46da63] | 239 | /** @}
|
---|
[b2951e2] | 240 | */
|
---|