[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 <malloc.h>
|
---|
| 41 | #include <vfs/vfs_sess.h>
|
---|
[2595dab] | 42 | #include <io/console.h>
|
---|
| 43 | #include <ipc/console.h>
|
---|
[afa6e74] | 44 |
|
---|
[79ae36dd] | 45 | console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
|
---|
[afa6e74] | 46 | {
|
---|
[79ae36dd] | 47 | console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
|
---|
| 48 | if (!ctrl)
|
---|
| 49 | return NULL;
|
---|
| 50 |
|
---|
| 51 | ctrl->input_sess = fsession(EXCHANGE_SERIALIZE, ifile);
|
---|
| 52 | if (!ctrl->input_sess) {
|
---|
| 53 | free(ctrl);
|
---|
| 54 | return NULL;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | ctrl->output_sess = fsession(EXCHANGE_SERIALIZE, ofile);
|
---|
| 58 | if (!ctrl->output_sess) {
|
---|
| 59 | free(ctrl);
|
---|
| 60 | return NULL;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | ctrl->input = ifile;
|
---|
| 64 | ctrl->output = ofile;
|
---|
| 65 | ctrl->input_aid = 0;
|
---|
| 66 |
|
---|
| 67 | return ctrl;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void console_done(console_ctrl_t *ctrl)
|
---|
| 71 | {
|
---|
| 72 | free(ctrl);
|
---|
[2595dab] | 73 | }
|
---|
| 74 |
|
---|
[79ae36dd] | 75 | bool console_kcon(void)
|
---|
[2595dab] | 76 | {
|
---|
[79ae36dd] | 77 | return __SYSCALL0(SYS_DEBUG_ACTIVATE_CONSOLE);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void console_flush(console_ctrl_t *ctrl)
|
---|
| 81 | {
|
---|
| 82 | fflush(ctrl->output);
|
---|
[2595dab] | 83 | }
|
---|
| 84 |
|
---|
[79ae36dd] | 85 | void console_clear(console_ctrl_t *ctrl)
|
---|
[2595dab] | 86 | {
|
---|
[79ae36dd] | 87 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 88 | async_req_0_0(exch, CONSOLE_CLEAR);
|
---|
[79ae36dd] | 89 | async_exchange_end(exch);
|
---|
[2595dab] | 90 | }
|
---|
| 91 |
|
---|
[79ae36dd] | 92 | int console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
|
---|
| 93 | {
|
---|
| 94 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 95 | int rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
|
---|
| 96 | async_exchange_end(exch);
|
---|
| 97 |
|
---|
| 98 | return rc;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | void console_set_style(console_ctrl_t *ctrl, uint8_t style)
|
---|
| 102 | {
|
---|
| 103 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 104 | async_req_1_0(exch, CONSOLE_SET_STYLE, style);
|
---|
[79ae36dd] | 105 | async_exchange_end(exch);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[7c014d1] | 108 | void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
|
---|
[9f1362d4] | 109 | uint8_t flags)
|
---|
[2595dab] | 110 | {
|
---|
[79ae36dd] | 111 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 112 | async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
|
---|
[79ae36dd] | 113 | async_exchange_end(exch);
|
---|
[d2cc7e1] | 114 | }
|
---|
| 115 |
|
---|
[7c014d1] | 116 | void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
|
---|
| 117 | uint32_t fgcolor)
|
---|
[0b6d70d] | 118 | {
|
---|
[79ae36dd] | 119 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[7c014d1] | 120 | async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
|
---|
[79ae36dd] | 121 | async_exchange_end(exch);
|
---|
[0b6d70d] | 122 | }
|
---|
| 123 |
|
---|
[79ae36dd] | 124 | void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
|
---|
[1c03c17] | 125 | {
|
---|
[79ae36dd] | 126 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[5d94b16c] | 127 | async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
|
---|
[79ae36dd] | 128 | async_exchange_end(exch);
|
---|
[2595dab] | 129 | }
|
---|
| 130 |
|
---|
[79ae36dd] | 131 | int console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
|
---|
[50cfa6c] | 132 | {
|
---|
[79ae36dd] | 133 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 134 | int rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
|
---|
| 135 | async_exchange_end(exch);
|
---|
| 136 |
|
---|
| 137 | return rc;
|
---|
[50cfa6c] | 138 | }
|
---|
| 139 |
|
---|
[79ae36dd] | 140 | int console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
|
---|
[2595dab] | 141 | {
|
---|
[79ae36dd] | 142 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
| 143 | int rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
|
---|
| 144 | async_exchange_end(exch);
|
---|
| 145 |
|
---|
| 146 | return rc;
|
---|
[2595dab] | 147 | }
|
---|
| 148 |
|
---|
[79ae36dd] | 149 | void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
|
---|
[19528516] | 150 | {
|
---|
[79ae36dd] | 151 | async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
|
---|
[5d94b16c] | 152 | async_req_2_0(exch, CONSOLE_SET_POS, col, row);
|
---|
[79ae36dd] | 153 | async_exchange_end(exch);
|
---|
[19528516] | 154 | }
|
---|
| 155 |
|
---|
[79ae36dd] | 156 | bool console_get_kbd_event(console_ctrl_t *ctrl, kbd_event_t *event)
|
---|
[2595dab] | 157 | {
|
---|
[79ae36dd] | 158 | if (ctrl->input_aid == 0) {
|
---|
| 159 | sysarg_t type;
|
---|
| 160 | sysarg_t key;
|
---|
| 161 | sysarg_t mods;
|
---|
| 162 | sysarg_t c;
|
---|
| 163 |
|
---|
| 164 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
| 165 | int rc = async_req_0_4(exch, CONSOLE_GET_EVENT, &type, &key, &mods, &c);
|
---|
| 166 | async_exchange_end(exch);
|
---|
| 167 |
|
---|
| 168 | if (rc != EOK) {
|
---|
| 169 | errno = rc;
|
---|
| 170 | return false;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | event->type = type;
|
---|
| 174 | event->key = key;
|
---|
| 175 | event->mods = mods;
|
---|
| 176 | event->c = c;
|
---|
| 177 | } else {
|
---|
| 178 | sysarg_t retval;
|
---|
| 179 | async_wait_for(ctrl->input_aid, &retval);
|
---|
| 180 |
|
---|
| 181 | ctrl->input_aid = 0;
|
---|
| 182 |
|
---|
| 183 | if (retval != EOK) {
|
---|
| 184 | errno = (int) retval;
|
---|
| 185 | return false;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | event->type = IPC_GET_ARG1(ctrl->input_call);
|
---|
| 189 | event->key = IPC_GET_ARG2(ctrl->input_call);
|
---|
| 190 | event->mods = IPC_GET_ARG3(ctrl->input_call);
|
---|
| 191 | event->c = IPC_GET_ARG4(ctrl->input_call);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return true;
|
---|
[2595dab] | 195 | }
|
---|
| 196 |
|
---|
[79ae36dd] | 197 | bool console_get_kbd_event_timeout(console_ctrl_t *ctrl, kbd_event_t *event,
|
---|
| 198 | suseconds_t *timeout)
|
---|
[2595dab] | 199 | {
|
---|
[79ae36dd] | 200 | struct timeval t0;
|
---|
| 201 | gettimeofday(&t0, NULL);
|
---|
| 202 |
|
---|
| 203 | if (ctrl->input_aid == 0) {
|
---|
| 204 | async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
|
---|
| 205 | ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
|
---|
| 206 | &ctrl->input_call);
|
---|
| 207 | async_exchange_end(exch);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | sysarg_t retval;
|
---|
| 211 | int rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
|
---|
| 212 | if (rc != EOK) {
|
---|
| 213 | *timeout = 0;
|
---|
| 214 | errno = rc;
|
---|
| 215 | return false;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | ctrl->input_aid = 0;
|
---|
[2595dab] | 219 |
|
---|
[79ae36dd] | 220 | if (retval != EOK) {
|
---|
| 221 | errno = (int) retval;
|
---|
[2595dab] | 222 | return false;
|
---|
[79ae36dd] | 223 | }
|
---|
| 224 |
|
---|
| 225 | event->type = IPC_GET_ARG1(ctrl->input_call);
|
---|
| 226 | event->key = IPC_GET_ARG2(ctrl->input_call);
|
---|
| 227 | event->mods = IPC_GET_ARG3(ctrl->input_call);
|
---|
| 228 | event->c = IPC_GET_ARG4(ctrl->input_call);
|
---|
[2595dab] | 229 |
|
---|
[79ae36dd] | 230 | /* Update timeout */
|
---|
| 231 | struct timeval t1;
|
---|
| 232 | gettimeofday(&t1, NULL);
|
---|
| 233 | *timeout -= tv_sub(&t1, &t0);
|
---|
[2595dab] | 234 |
|
---|
| 235 | return true;
|
---|
[1c03c17] | 236 | }
|
---|
| 237 |
|
---|
[a46da63] | 238 | /** @}
|
---|
[b2951e2] | 239 | */
|
---|