[51c1b003] | 1 | /*
|
---|
[7c014d1] | 2 | * Copyright (c) 2011 Martin Decky
|
---|
[51c1b003] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
[ce5bcb4] | 28 |
|
---|
[231a60a] | 29 | /** @addtogroup console
|
---|
[1601f3c] | 30 | * @{
|
---|
[ce5bcb4] | 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[7c014d1] | 35 | #include <async.h>
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 | #include <adt/prodcons.h>
|
---|
[111d2d6] | 38 | #include <io/input.h>
|
---|
[7c014d1] | 39 | #include <ipc/vfs.h>
|
---|
[51c1b003] | 40 | #include <errno.h>
|
---|
[30db06c] | 41 | #include <str_error.h>
|
---|
[15f3c3f] | 42 | #include <loc.h>
|
---|
[5d94b16c] | 43 | #include <io/con_srv.h>
|
---|
[111d2d6] | 44 | #include <io/kbd_event.h>
|
---|
[7c014d1] | 45 | #include <io/keycode.h>
|
---|
[6d5e378] | 46 | #include <io/chargrid.h>
|
---|
| 47 | #include <io/output.h>
|
---|
[7c014d1] | 48 | #include <align.h>
|
---|
| 49 | #include <malloc.h>
|
---|
| 50 | #include <as.h>
|
---|
[1e4cada] | 51 | #include <fibril_synch.h>
|
---|
[9805cde] | 52 | #include "console.h"
|
---|
[369a5f8] | 53 |
|
---|
[1313ee9] | 54 | #define NAME "console"
|
---|
| 55 | #define NAMESPACE "term"
|
---|
[79ae36dd] | 56 |
|
---|
[6d5e378] | 57 | #define UTF8_CHAR_BUFFER_SIZE (STR_BOUNDS(1) + 1)
|
---|
[a58bc8b] | 58 |
|
---|
[79460ae] | 59 | typedef struct {
|
---|
[6d5e378] | 60 | atomic_t refcnt; /**< Connection reference count */
|
---|
| 61 | prodcons_t input_pc; /**< Incoming keyboard events */
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Not yet sent bytes of last char event.
|
---|
| 65 | */
|
---|
| 66 | char char_remains[UTF8_CHAR_BUFFER_SIZE];
|
---|
| 67 | size_t char_remains_len; /**< Number of not yet sent bytes. */
|
---|
[7c014d1] | 68 |
|
---|
[6d5e378] | 69 | fibril_mutex_t mtx; /**< Lock protecting mutable fields */
|
---|
[7c014d1] | 70 |
|
---|
[6d5e378] | 71 | size_t index; /**< Console index */
|
---|
| 72 | service_id_t dsid; /**< Service handle */
|
---|
[7c014d1] | 73 |
|
---|
[6d5e378] | 74 | sysarg_t cols; /**< Number of columns */
|
---|
| 75 | sysarg_t rows; /**< Number of rows */
|
---|
| 76 | console_caps_t ccaps; /**< Console capabilities */
|
---|
[7c014d1] | 77 |
|
---|
[6d5e378] | 78 | chargrid_t *frontbuf; /**< Front buffer */
|
---|
| 79 | frontbuf_handle_t fbid; /**< Front buffer handle */
|
---|
[5d94b16c] | 80 | con_srvs_t srvs; /**< Console service setup */
|
---|
[424cd43] | 81 | } console_t;
|
---|
[79460ae] | 82 |
|
---|
[111d2d6] | 83 | /** Input server proxy */
|
---|
| 84 | static input_t *input;
|
---|
[593e023] | 85 | static bool active = false;
|
---|
[7c014d1] | 86 |
|
---|
[6d5e378] | 87 | /** Session to the output server */
|
---|
| 88 | static async_sess_t *output_sess;
|
---|
[7c014d1] | 89 |
|
---|
[6d5e378] | 90 | /** Output dimensions */
|
---|
| 91 | static sysarg_t cols;
|
---|
| 92 | static sysarg_t rows;
|
---|
[7c014d1] | 93 |
|
---|
[1601f3c] | 94 | /** Array of data for virtual consoles */
|
---|
[424cd43] | 95 | static console_t consoles[CONSOLE_COUNT];
|
---|
| 96 |
|
---|
[7c014d1] | 97 | /** Mutex for console switching */
|
---|
| 98 | static FIBRIL_MUTEX_INITIALIZE(switch_mtx);
|
---|
| 99 |
|
---|
| 100 | static console_t *active_console = &consoles[0];
|
---|
[1601f3c] | 101 |
|
---|
[593e023] | 102 | static int input_ev_active(input_t *);
|
---|
| 103 | static int input_ev_deactive(input_t *);
|
---|
[111d2d6] | 104 | static int input_ev_key(input_t *, kbd_event_type_t, keycode_t, keymod_t, wchar_t);
|
---|
| 105 | static int input_ev_move(input_t *, int, int);
|
---|
| 106 | static int input_ev_abs_move(input_t *, unsigned, unsigned, unsigned, unsigned);
|
---|
| 107 | static int input_ev_button(input_t *, int, int);
|
---|
| 108 |
|
---|
| 109 | static input_ev_ops_t input_ev_ops = {
|
---|
[593e023] | 110 | .active = input_ev_active,
|
---|
| 111 | .deactive = input_ev_deactive,
|
---|
[111d2d6] | 112 | .key = input_ev_key,
|
---|
| 113 | .move = input_ev_move,
|
---|
| 114 | .abs_move = input_ev_abs_move,
|
---|
| 115 | .button = input_ev_button
|
---|
| 116 | };
|
---|
| 117 |
|
---|
[5d94b16c] | 118 | static int cons_open(con_srvs_t *, con_srv_t *);
|
---|
| 119 | static int cons_close(con_srv_t *);
|
---|
| 120 | static int cons_read(con_srv_t *, void *, size_t);
|
---|
| 121 | static int cons_write(con_srv_t *, void *, size_t);
|
---|
| 122 | static void cons_sync(con_srv_t *);
|
---|
| 123 | static void cons_clear(con_srv_t *);
|
---|
| 124 | static void cons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
|
---|
| 125 | static int cons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 126 | static int cons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 127 | static int cons_get_color_cap(con_srv_t *, console_caps_t *);
|
---|
| 128 | static void cons_set_style(con_srv_t *, console_style_t);
|
---|
| 129 | static void cons_set_color(con_srv_t *, console_color_t, console_color_t,
|
---|
| 130 | console_color_attr_t);
|
---|
| 131 | static void cons_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
|
---|
| 132 | static void cons_set_cursor_visibility(con_srv_t *, bool);
|
---|
[902f0906] | 133 | static int cons_get_event(con_srv_t *, cons_event_t *);
|
---|
[5d94b16c] | 134 |
|
---|
| 135 | static con_ops_t con_ops = {
|
---|
| 136 | .open = cons_open,
|
---|
| 137 | .close = cons_close,
|
---|
| 138 | .read = cons_read,
|
---|
| 139 | .write = cons_write,
|
---|
| 140 | .sync = cons_sync,
|
---|
| 141 | .clear = cons_clear,
|
---|
| 142 | .set_pos = cons_set_pos,
|
---|
| 143 | .get_pos = cons_get_pos,
|
---|
| 144 | .get_size = cons_get_size,
|
---|
| 145 | .get_color_cap = cons_get_color_cap,
|
---|
| 146 | .set_style = cons_set_style,
|
---|
| 147 | .set_color = cons_set_color,
|
---|
| 148 | .set_rgb_color = cons_set_rgb_color,
|
---|
| 149 | .set_cursor_visibility = cons_set_cursor_visibility,
|
---|
| 150 | .get_event = cons_get_event
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | static console_t *srv_to_console(con_srv_t *srv)
|
---|
| 154 | {
|
---|
| 155 | return srv->srvs->sarg;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[7c014d1] | 158 | static void cons_update(console_t *cons)
|
---|
[9805cde] | 159 | {
|
---|
[7c014d1] | 160 | fibril_mutex_lock(&switch_mtx);
|
---|
| 161 | fibril_mutex_lock(&cons->mtx);
|
---|
| 162 |
|
---|
[593e023] | 163 | if ((active) && (cons == active_console)) {
|
---|
[6d5e378] | 164 | output_update(output_sess, cons->fbid);
|
---|
| 165 | output_cursor_update(output_sess, cons->fbid);
|
---|
[7c014d1] | 166 | }
|
---|
| 167 |
|
---|
| 168 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 169 | fibril_mutex_unlock(&switch_mtx);
|
---|
[9805cde] | 170 | }
|
---|
| 171 |
|
---|
[7c014d1] | 172 | static void cons_update_cursor(console_t *cons)
|
---|
[9805cde] | 173 | {
|
---|
[7c014d1] | 174 | fibril_mutex_lock(&switch_mtx);
|
---|
| 175 | fibril_mutex_lock(&cons->mtx);
|
---|
| 176 |
|
---|
[593e023] | 177 | if ((active) && (cons == active_console))
|
---|
[6d5e378] | 178 | output_cursor_update(output_sess, cons->fbid);
|
---|
[7c014d1] | 179 |
|
---|
| 180 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 181 | fibril_mutex_unlock(&switch_mtx);
|
---|
[429acb9] | 182 | }
|
---|
| 183 |
|
---|
[6d5e378] | 184 | static void cons_damage(console_t *cons)
|
---|
[d2cc7e1] | 185 | {
|
---|
[7c014d1] | 186 | fibril_mutex_lock(&switch_mtx);
|
---|
| 187 | fibril_mutex_lock(&cons->mtx);
|
---|
| 188 |
|
---|
[593e023] | 189 | if ((active) && (cons == active_console)) {
|
---|
[6d5e378] | 190 | output_damage(output_sess, cons->fbid, 0, 0, cons->cols,
|
---|
[7c014d1] | 191 | cons->rows);
|
---|
[6d5e378] | 192 | output_cursor_update(output_sess, cons->fbid);
|
---|
[dc033a1] | 193 | }
|
---|
[7c014d1] | 194 |
|
---|
| 195 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 196 | fibril_mutex_unlock(&switch_mtx);
|
---|
[d2cc7e1] | 197 | }
|
---|
| 198 |
|
---|
[593e023] | 199 | static void cons_switch(unsigned int index)
|
---|
[d2cc7e1] | 200 | {
|
---|
[593e023] | 201 | /*
|
---|
| 202 | * The first undefined index is reserved
|
---|
| 203 | * for switching to the kernel console.
|
---|
| 204 | */
|
---|
| 205 | if (index == CONSOLE_COUNT) {
|
---|
| 206 | if (console_kcon())
|
---|
| 207 | active = false;
|
---|
| 208 |
|
---|
| 209 | return;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | if (index > CONSOLE_COUNT)
|
---|
| 213 | return;
|
---|
| 214 |
|
---|
| 215 | console_t *cons = &consoles[index];
|
---|
| 216 |
|
---|
[7c014d1] | 217 | fibril_mutex_lock(&switch_mtx);
|
---|
| 218 |
|
---|
| 219 | if (cons == active_console) {
|
---|
| 220 | fibril_mutex_unlock(&switch_mtx);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | active_console = cons;
|
---|
| 225 |
|
---|
| 226 | fibril_mutex_unlock(&switch_mtx);
|
---|
| 227 |
|
---|
[6d5e378] | 228 | cons_damage(cons);
|
---|
[d2cc7e1] | 229 | }
|
---|
| 230 |
|
---|
[593e023] | 231 | static int input_ev_active(input_t *input)
|
---|
[024fcc5] | 232 | {
|
---|
[593e023] | 233 | active = true;
|
---|
| 234 | output_claim(output_sess);
|
---|
| 235 | cons_damage(active_console);
|
---|
[7c014d1] | 236 |
|
---|
[593e023] | 237 | return EOK;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | static int input_ev_deactive(input_t *input)
|
---|
| 241 | {
|
---|
| 242 | active = false;
|
---|
| 243 | output_yield(output_sess);
|
---|
[7c014d1] | 244 |
|
---|
[593e023] | 245 | return EOK;
|
---|
[429acb9] | 246 | }
|
---|
[10569b1] | 247 |
|
---|
[111d2d6] | 248 | static int input_ev_key(input_t *input, kbd_event_type_t type, keycode_t key,
|
---|
| 249 | keymod_t mods, wchar_t c)
|
---|
[51c1b003] | 250 | {
|
---|
[593e023] | 251 | if ((key >= KC_F1) && (key <= KC_F1 + CONSOLE_COUNT) &&
|
---|
[111d2d6] | 252 | ((mods & KM_CTRL) == 0)) {
|
---|
[593e023] | 253 | cons_switch(key - KC_F1);
|
---|
[111d2d6] | 254 | } else {
|
---|
| 255 | /* Got key press/release event */
|
---|
| 256 | kbd_event_t *event =
|
---|
| 257 | (kbd_event_t *) malloc(sizeof(kbd_event_t));
|
---|
| 258 | if (event == NULL) {
|
---|
| 259 | return ENOMEM;
|
---|
[79ae36dd] | 260 | }
|
---|
| 261 |
|
---|
[111d2d6] | 262 | link_initialize(&event->link);
|
---|
| 263 | event->type = type;
|
---|
| 264 | event->key = key;
|
---|
| 265 | event->mods = mods;
|
---|
| 266 | event->c = c;
|
---|
[7c014d1] | 267 |
|
---|
[593e023] | 268 | prodcons_produce(&active_console->input_pc,
|
---|
[111d2d6] | 269 | &event->link);
|
---|
[7c014d1] | 270 | }
|
---|
[111d2d6] | 271 |
|
---|
| 272 | return EOK;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | static int input_ev_move(input_t *input, int dx, int dy)
|
---|
| 276 | {
|
---|
| 277 | return EOK;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | static int input_ev_abs_move(input_t *input, unsigned x , unsigned y,
|
---|
| 281 | unsigned max_x, unsigned max_y)
|
---|
| 282 | {
|
---|
| 283 | return EOK;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | static int input_ev_button(input_t *input, int bnum, int bpress)
|
---|
| 287 | {
|
---|
| 288 | return EOK;
|
---|
[7c014d1] | 289 | }
|
---|
[1875a0c] | 290 |
|
---|
[7c014d1] | 291 | /** Process a character from the client (TTY emulation). */
|
---|
| 292 | static void cons_write_char(console_t *cons, wchar_t ch)
|
---|
| 293 | {
|
---|
| 294 | sysarg_t updated = 0;
|
---|
| 295 |
|
---|
| 296 | fibril_mutex_lock(&cons->mtx);
|
---|
| 297 |
|
---|
| 298 | switch (ch) {
|
---|
| 299 | case '\n':
|
---|
[6d5e378] | 300 | updated = chargrid_newline(cons->frontbuf);
|
---|
[7c014d1] | 301 | break;
|
---|
| 302 | case '\r':
|
---|
| 303 | break;
|
---|
| 304 | case '\t':
|
---|
[6d5e378] | 305 | updated = chargrid_tabstop(cons->frontbuf, 8);
|
---|
[7c014d1] | 306 | break;
|
---|
| 307 | case '\b':
|
---|
[6d5e378] | 308 | updated = chargrid_backspace(cons->frontbuf);
|
---|
[7c014d1] | 309 | break;
|
---|
| 310 | default:
|
---|
[6d5e378] | 311 | updated = chargrid_putchar(cons->frontbuf, ch, true);
|
---|
[9f51afc] | 312 | }
|
---|
[7c014d1] | 313 |
|
---|
| 314 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 315 |
|
---|
| 316 | if (updated > 1)
|
---|
| 317 | cons_update(cons);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[5d94b16c] | 320 | static void cons_set_cursor_vis(console_t *cons, bool visible)
|
---|
[7c014d1] | 321 | {
|
---|
| 322 | fibril_mutex_lock(&cons->mtx);
|
---|
[6d5e378] | 323 | chargrid_set_cursor_visibility(cons->frontbuf, visible);
|
---|
[7c014d1] | 324 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 325 |
|
---|
| 326 | cons_update_cursor(cons);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[5d94b16c] | 329 | static int cons_open(con_srvs_t *srvs, con_srv_t *srv)
|
---|
[7c014d1] | 330 | {
|
---|
[5d94b16c] | 331 | return EOK;
|
---|
[9f51afc] | 332 | }
|
---|
| 333 |
|
---|
[5d94b16c] | 334 | static int cons_close(con_srv_t *srv)
|
---|
[d2cc7e1] | 335 | {
|
---|
[5d94b16c] | 336 | return EOK;
|
---|
[424cd43] | 337 | }
|
---|
| 338 |
|
---|
[5d94b16c] | 339 | static int cons_read(con_srv_t *srv, void *buf, size_t size)
|
---|
[424cd43] | 340 | {
|
---|
[5d94b16c] | 341 | uint8_t *bbuf = buf;
|
---|
| 342 | console_t *cons = srv_to_console(srv);
|
---|
[424cd43] | 343 | size_t pos = 0;
|
---|
[e435537] | 344 |
|
---|
[a58bc8b] | 345 | /*
|
---|
| 346 | * Read input from keyboard and copy it to the buffer.
|
---|
| 347 | * We need to handle situation when wchar is split by 2 following
|
---|
| 348 | * reads.
|
---|
| 349 | */
|
---|
[7c014d1] | 350 | while (pos < size) {
|
---|
[a58bc8b] | 351 | /* Copy to the buffer remaining characters. */
|
---|
| 352 | while ((pos < size) && (cons->char_remains_len > 0)) {
|
---|
[5d94b16c] | 353 | bbuf[pos] = cons->char_remains[0];
|
---|
[424cd43] | 354 | pos++;
|
---|
[e435537] | 355 |
|
---|
[a58bc8b] | 356 | /* Unshift the array. */
|
---|
[e435537] | 357 | for (size_t i = 1; i < cons->char_remains_len; i++)
|
---|
[a58bc8b] | 358 | cons->char_remains[i - 1] = cons->char_remains[i];
|
---|
[e435537] | 359 |
|
---|
[a58bc8b] | 360 | cons->char_remains_len--;
|
---|
| 361 | }
|
---|
[e435537] | 362 |
|
---|
[a58bc8b] | 363 | /* Still not enough? Then get another key from the queue. */
|
---|
| 364 | if (pos < size) {
|
---|
| 365 | link_t *link = prodcons_consume(&cons->input_pc);
|
---|
| 366 | kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
|
---|
[e435537] | 367 |
|
---|
[a58bc8b] | 368 | /* Accept key presses of printable chars only. */
|
---|
| 369 | if ((event->type == KEY_PRESS) && (event->c != 0)) {
|
---|
| 370 | wchar_t tmp[2] = { event->c, 0 };
|
---|
| 371 | wstr_to_str(cons->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
|
---|
| 372 | cons->char_remains_len = str_size(cons->char_remains);
|
---|
| 373 | }
|
---|
[e435537] | 374 |
|
---|
[a58bc8b] | 375 | free(event);
|
---|
[424cd43] | 376 | }
|
---|
| 377 | }
|
---|
[ce3efa0] | 378 |
|
---|
[5d94b16c] | 379 | return size;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | static int cons_write(con_srv_t *srv, void *data, size_t size)
|
---|
| 383 | {
|
---|
| 384 | console_t *cons = srv_to_console(srv);
|
---|
| 385 |
|
---|
| 386 | size_t off = 0;
|
---|
| 387 | while (off < size)
|
---|
| 388 | cons_write_char(cons, str_decode(data, &off, size));
|
---|
[ce3efa0] | 389 |
|
---|
[5d94b16c] | 390 | return size;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | static void cons_sync(con_srv_t *srv)
|
---|
| 394 | {
|
---|
| 395 | console_t *cons = srv_to_console(srv);
|
---|
[424cd43] | 396 |
|
---|
[5d94b16c] | 397 | cons_update(cons);
|
---|
[424cd43] | 398 | }
|
---|
| 399 |
|
---|
[5d94b16c] | 400 | static void cons_clear(con_srv_t *srv)
|
---|
[424cd43] | 401 | {
|
---|
[5d94b16c] | 402 | console_t *cons = srv_to_console(srv);
|
---|
| 403 |
|
---|
| 404 | fibril_mutex_lock(&cons->mtx);
|
---|
| 405 | chargrid_clear(cons->frontbuf);
|
---|
| 406 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 407 |
|
---|
| 408 | cons_update(cons);
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | static void cons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
|
---|
| 412 | {
|
---|
| 413 | console_t *cons = srv_to_console(srv);
|
---|
| 414 |
|
---|
| 415 | fibril_mutex_lock(&cons->mtx);
|
---|
| 416 | chargrid_set_cursor(cons->frontbuf, col, row);
|
---|
| 417 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 418 |
|
---|
| 419 | cons_update_cursor(cons);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | static int cons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
---|
| 423 | {
|
---|
| 424 | console_t *cons = srv_to_console(srv);
|
---|
| 425 |
|
---|
| 426 | fibril_mutex_lock(&cons->mtx);
|
---|
| 427 | chargrid_get_cursor(cons->frontbuf, col, row);
|
---|
| 428 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 429 |
|
---|
| 430 | return EOK;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | static int cons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
---|
| 434 | {
|
---|
| 435 | console_t *cons = srv_to_console(srv);
|
---|
| 436 |
|
---|
| 437 | fibril_mutex_lock(&cons->mtx);
|
---|
| 438 | *cols = cons->cols;
|
---|
| 439 | *rows = cons->rows;
|
---|
| 440 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 441 |
|
---|
| 442 | return EOK;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | static int cons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
|
---|
| 446 | {
|
---|
| 447 | console_t *cons = srv_to_console(srv);
|
---|
| 448 |
|
---|
| 449 | fibril_mutex_lock(&cons->mtx);
|
---|
| 450 | *ccaps = cons->ccaps;
|
---|
| 451 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 452 |
|
---|
| 453 | return EOK;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | static void cons_set_style(con_srv_t *srv, console_style_t style)
|
---|
| 457 | {
|
---|
| 458 | console_t *cons = srv_to_console(srv);
|
---|
| 459 |
|
---|
[7c014d1] | 460 | fibril_mutex_lock(&cons->mtx);
|
---|
[6d5e378] | 461 | chargrid_set_style(cons->frontbuf, style);
|
---|
[7c014d1] | 462 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[5d94b16c] | 465 | static void cons_set_color(con_srv_t *srv, console_color_t bgcolor,
|
---|
[7c014d1] | 466 | console_color_t fgcolor, console_color_attr_t attr)
|
---|
| 467 | {
|
---|
[5d94b16c] | 468 | console_t *cons = srv_to_console(srv);
|
---|
| 469 |
|
---|
[7c014d1] | 470 | fibril_mutex_lock(&cons->mtx);
|
---|
[6d5e378] | 471 | chargrid_set_color(cons->frontbuf, bgcolor, fgcolor, attr);
|
---|
[7c014d1] | 472 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[5d94b16c] | 475 | static void cons_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
|
---|
[7c014d1] | 476 | pixel_t fgcolor)
|
---|
| 477 | {
|
---|
[5d94b16c] | 478 | console_t *cons = srv_to_console(srv);
|
---|
| 479 |
|
---|
[7c014d1] | 480 | fibril_mutex_lock(&cons->mtx);
|
---|
[6d5e378] | 481 | chargrid_set_rgb_color(cons->frontbuf, bgcolor, fgcolor);
|
---|
[7c014d1] | 482 | fibril_mutex_unlock(&cons->mtx);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[5d94b16c] | 485 | static void cons_set_cursor_visibility(con_srv_t *srv, bool visible)
|
---|
| 486 | {
|
---|
| 487 | console_t *cons = srv_to_console(srv);
|
---|
| 488 |
|
---|
| 489 | cons_set_cursor_vis(cons, visible);
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[902f0906] | 492 | static int cons_get_event(con_srv_t *srv, cons_event_t *event)
|
---|
[7c014d1] | 493 | {
|
---|
[5d94b16c] | 494 | console_t *cons = srv_to_console(srv);
|
---|
[7c014d1] | 495 | link_t *link = prodcons_consume(&cons->input_pc);
|
---|
[5d94b16c] | 496 | kbd_event_t *kevent = list_get_instance(link, kbd_event_t, link);
|
---|
[9f1362d4] | 497 |
|
---|
[902f0906] | 498 | event->type = CEV_KEY;
|
---|
| 499 | event->ev.key = *kevent;
|
---|
[ce3efa0] | 500 |
|
---|
[5d94b16c] | 501 | free(kevent);
|
---|
| 502 | return EOK;
|
---|
[d2cc7e1] | 503 | }
|
---|
| 504 |
|
---|
[9934f7d] | 505 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[eaf34f7] | 506 | {
|
---|
[424cd43] | 507 | console_t *cons = NULL;
|
---|
[3ad953c] | 508 |
|
---|
[7c014d1] | 509 | for (size_t i = 0; i < CONSOLE_COUNT; i++) {
|
---|
| 510 | if (consoles[i].dsid == (service_id_t) IPC_GET_ARG1(*icall)) {
|
---|
[424cd43] | 511 | cons = &consoles[i];
|
---|
| 512 | break;
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | if (cons == NULL) {
|
---|
[ffa2c8ef] | 517 | async_answer_0(iid, ENOENT);
|
---|
[eaf34f7] | 518 | return;
|
---|
| 519 | }
|
---|
[424cd43] | 520 |
|
---|
[6d5e378] | 521 | if (atomic_postinc(&cons->refcnt) == 0)
|
---|
[5d94b16c] | 522 | cons_set_cursor_vis(cons, true);
|
---|
[3ad953c] | 523 |
|
---|
[5d94b16c] | 524 | con_conn(iid, icall, &cons->srvs);
|
---|
[eaf34f7] | 525 | }
|
---|
| 526 |
|
---|
[111d2d6] | 527 | static int input_connect(const char *svc)
|
---|
[eaf34f7] | 528 | {
|
---|
[a40dea3] | 529 | async_sess_t *sess;
|
---|
[7c014d1] | 530 | service_id_t dsid;
|
---|
[9f1362d4] | 531 |
|
---|
[7c014d1] | 532 | int rc = loc_service_get_id(svc, &dsid, 0);
|
---|
[111d2d6] | 533 | if (rc != EOK) {
|
---|
| 534 | printf("%s: Input service %s not found\n", NAME, svc);
|
---|
| 535 | return rc;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | sess = loc_service_connect(EXCHANGE_ATOMIC, dsid, 0);
|
---|
| 539 | if (sess == NULL) {
|
---|
| 540 | printf("%s: Unable to connect to input service %s\n", NAME,
|
---|
| 541 | svc);
|
---|
| 542 | return EIO;
|
---|
| 543 | }
|
---|
[7c014d1] | 544 |
|
---|
[111d2d6] | 545 | rc = input_open(sess, &input_ev_ops, NULL, &input);
|
---|
[700af62] | 546 | if (rc != EOK) {
|
---|
[a40dea3] | 547 | async_hangup(sess);
|
---|
[111d2d6] | 548 | printf("%s: Unable to communicate with service %s (%s)\n",
|
---|
[7c014d1] | 549 | NAME, svc, str_error(rc));
|
---|
[111d2d6] | 550 | return rc;
|
---|
[4904de8] | 551 | }
|
---|
[9f1362d4] | 552 |
|
---|
[111d2d6] | 553 | return EOK;
|
---|
[700af62] | 554 | }
|
---|
| 555 |
|
---|
[6d5e378] | 556 | static async_sess_t *output_connect(const char *svc)
|
---|
[700af62] | 557 | {
|
---|
[7c014d1] | 558 | async_sess_t *sess;
|
---|
| 559 | service_id_t dsid;
|
---|
| 560 |
|
---|
| 561 | int rc = loc_service_get_id(svc, &dsid, 0);
|
---|
| 562 | if (rc == EOK) {
|
---|
| 563 | sess = loc_service_connect(EXCHANGE_SERIALIZE, dsid, 0);
|
---|
| 564 | if (sess == NULL) {
|
---|
[6d5e378] | 565 | printf("%s: Unable to connect to output service %s\n",
|
---|
[7c014d1] | 566 | NAME, svc);
|
---|
| 567 | return NULL;
|
---|
| 568 | }
|
---|
| 569 | } else
|
---|
| 570 | return NULL;
|
---|
| 571 |
|
---|
| 572 | return sess;
|
---|
| 573 | }
|
---|
| 574 |
|
---|
[6d5e378] | 575 | static bool console_srv_init(char *input_svc, char *output_svc)
|
---|
[7c014d1] | 576 | {
|
---|
| 577 | /* Connect to input service */
|
---|
[ce3efa0] | 578 | int rc = input_connect(input_svc);
|
---|
[111d2d6] | 579 | if (rc != EOK)
|
---|
[700af62] | 580 | return false;
|
---|
[79ae36dd] | 581 |
|
---|
[6d5e378] | 582 | /* Connect to output service */
|
---|
| 583 | output_sess = output_connect(output_svc);
|
---|
| 584 | if (output_sess == NULL)
|
---|
[79ae36dd] | 585 | return false;
|
---|
[9f1362d4] | 586 |
|
---|
[15f3c3f] | 587 | /* Register server */
|
---|
[b688fd8] | 588 | async_set_fallback_port_handler(client_connection, NULL);
|
---|
[111d2d6] | 589 | rc = loc_server_register(NAME);
|
---|
[2f90b46] | 590 | if (rc != EOK) {
|
---|
[7c014d1] | 591 | printf("%s: Unable to register server (%s)\n", NAME,
|
---|
| 592 | str_error(rc));
|
---|
[424cd43] | 593 | return false;
|
---|
| 594 | }
|
---|
[516ff92] | 595 |
|
---|
[6d5e378] | 596 | output_get_dimensions(output_sess, &cols, &rows);
|
---|
| 597 | output_set_style(output_sess, STYLE_NORMAL);
|
---|
[7c014d1] | 598 |
|
---|
| 599 | console_caps_t ccaps;
|
---|
[6d5e378] | 600 | output_get_caps(output_sess, &ccaps);
|
---|
[3ad953c] | 601 |
|
---|
[593e023] | 602 | /*
|
---|
| 603 | * Inititalize consoles only if there are
|
---|
| 604 | * actually some output devices.
|
---|
| 605 | */
|
---|
| 606 | if (ccaps != 0) {
|
---|
| 607 | for (size_t i = 0; i < CONSOLE_COUNT; i++) {
|
---|
| 608 | consoles[i].index = i;
|
---|
| 609 | atomic_set(&consoles[i].refcnt, 0);
|
---|
| 610 | fibril_mutex_initialize(&consoles[i].mtx);
|
---|
| 611 | prodcons_initialize(&consoles[i].input_pc);
|
---|
| 612 | consoles[i].char_remains_len = 0;
|
---|
| 613 |
|
---|
| 614 | consoles[i].cols = cols;
|
---|
| 615 | consoles[i].rows = rows;
|
---|
| 616 | consoles[i].ccaps = ccaps;
|
---|
| 617 | consoles[i].frontbuf =
|
---|
| 618 | chargrid_create(cols, rows, CHARGRID_FLAG_SHARED);
|
---|
| 619 |
|
---|
| 620 | if (consoles[i].frontbuf == NULL) {
|
---|
| 621 | printf("%s: Unable to allocate frontbuffer %zu\n", NAME, i);
|
---|
| 622 | return false;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | consoles[i].fbid = output_frontbuf_create(output_sess,
|
---|
| 626 | consoles[i].frontbuf);
|
---|
| 627 | if (consoles[i].fbid == 0) {
|
---|
| 628 | printf("%s: Unable to create frontbuffer %zu\n", NAME, i);
|
---|
| 629 | return false;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | con_srvs_init(&consoles[i].srvs);
|
---|
| 633 | consoles[i].srvs.ops = &con_ops;
|
---|
| 634 | consoles[i].srvs.sarg = &consoles[i];
|
---|
| 635 |
|
---|
| 636 | char vc[LOC_NAME_MAXLEN + 1];
|
---|
| 637 | snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
|
---|
| 638 |
|
---|
| 639 | if (loc_service_register(vc, &consoles[i].dsid) != EOK) {
|
---|
| 640 | printf("%s: Unable to register device %s\n", NAME, vc);
|
---|
| 641 | return false;
|
---|
| 642 | }
|
---|
[7c014d1] | 643 | }
|
---|
| 644 |
|
---|
[593e023] | 645 | input_activate(input);
|
---|
[424cd43] | 646 | }
|
---|
| 647 |
|
---|
| 648 | return true;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[6d5e378] | 651 | static void usage(char *name)
|
---|
[47a350f] | 652 | {
|
---|
[6d5e378] | 653 | printf("Usage: %s <input_dev> <output_dev>\n", name);
|
---|
[47a350f] | 654 | }
|
---|
| 655 |
|
---|
[424cd43] | 656 | int main(int argc, char *argv[])
|
---|
| 657 | {
|
---|
[7c014d1] | 658 | if (argc < 3) {
|
---|
[6d5e378] | 659 | usage(argv[0]);
|
---|
[47a350f] | 660 | return -1;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
[7c014d1] | 663 | printf("%s: HelenOS Console service\n", NAME);
|
---|
[424cd43] | 664 |
|
---|
[7c014d1] | 665 | if (!console_srv_init(argv[1], argv[2]))
|
---|
[424cd43] | 666 | return -1;
|
---|
[79ae36dd] | 667 |
|
---|
[7c014d1] | 668 | printf("%s: Accepting connections\n", NAME);
|
---|
| 669 | task_retval(0);
|
---|
[eaf34f7] | 670 | async_manager();
|
---|
[3ad953c] | 671 |
|
---|
[6d5e378] | 672 | /* Never reached */
|
---|
[3ad953c] | 673 | return 0;
|
---|
[51c1b003] | 674 | }
|
---|
[516ff92] | 675 |
|
---|
[ce5bcb4] | 676 | /** @}
|
---|
| 677 | */
|
---|