| [51c1b003] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
|---|
| [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
|
|---|
| [ce5bcb4] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [3209923] | 35 | #include <libc.h>
|
|---|
| [79460ae] | 36 | #include <fb.h>
|
|---|
| [51c1b003] | 37 | #include <ipc/ipc.h>
|
|---|
| [24ff4df] | 38 | #include <kbd.h>
|
|---|
| [f89979b] | 39 | #include <kbd/keycode.h>
|
|---|
| [79460ae] | 40 | #include <ipc/fb.h>
|
|---|
| [51c1b003] | 41 | #include <ipc/services.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| [79460ae] | 43 | #include <key_buffer.h>
|
|---|
| [d3e6935] | 44 | #include <ipc/console.h>
|
|---|
| [eaf34f7] | 45 | #include <unistd.h>
|
|---|
| 46 | #include <async.h>
|
|---|
| [cf28036c] | 47 | #include <libadt/fifo.h>
|
|---|
| [3993b3d] | 48 | #include <screenbuffer.h>
|
|---|
| [2def788] | 49 | #include <sys/mman.h>
|
|---|
| [271b540] | 50 | #include <stdio.h>
|
|---|
| [3ad953c] | 51 | #include <sysinfo.h>
|
|---|
| [79460ae] | 52 |
|
|---|
| [9805cde] | 53 | #include "console.h"
|
|---|
| [e1c4849] | 54 | #include "gcons.h"
|
|---|
| 55 |
|
|---|
| [cf28036c] | 56 | #define MAX_KEYREQUESTS_BUFFERED 32
|
|---|
| [51c1b003] | 57 |
|
|---|
| [271b540] | 58 | #define NAME "console"
|
|---|
| [51c1b003] | 59 |
|
|---|
| [e87e18f] | 60 | /** Index of currently used virtual console.
|
|---|
| 61 | */
|
|---|
| [390a678] | 62 | int active_console = 0;
|
|---|
| [3ad953c] | 63 | int prev_console = 0;
|
|---|
| [eaf34f7] | 64 |
|
|---|
| [dc033a1] | 65 | /** Information about framebuffer */
|
|---|
| [3993b3d] | 66 | struct {
|
|---|
| 67 | int phone; /**< Framebuffer phone */
|
|---|
| [bb51e9a8] | 68 | ipcarg_t rows; /**< Framebuffer rows */
|
|---|
| 69 | ipcarg_t cols; /**< Framebuffer columns */
|
|---|
| [3993b3d] | 70 | } fb_info;
|
|---|
| 71 |
|
|---|
| [79460ae] | 72 | typedef struct {
|
|---|
| [e87e18f] | 73 | keybuffer_t keybuffer; /**< Buffer for incoming keys. */
|
|---|
| [00bb6965] | 74 | /** Buffer for unsatisfied request for keys. */
|
|---|
| 75 | FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
|
|---|
| 76 | MAX_KEYREQUESTS_BUFFERED);
|
|---|
| [e87e18f] | 77 | int keyrequest_counter; /**< Number of requests in buffer. */
|
|---|
| 78 | int client_phone; /**< Phone to connected client. */
|
|---|
| [00bb6965] | 79 | int used; /**< 1 if this virtual console is
|
|---|
| 80 | * connected to some client.*/
|
|---|
| 81 | screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen
|
|---|
| 82 | * contents and related settings. */
|
|---|
| [79460ae] | 83 | } connection_t;
|
|---|
| 84 |
|
|---|
| [00bb6965] | 85 | static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
|
|---|
| 86 | * consoles */
|
|---|
| 87 | static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared
|
|---|
| 88 | * with framebufer used for
|
|---|
| 89 | * faster virtual console
|
|---|
| [c738d65] | 90 | * switching */
|
|---|
| [d2cc7e1] | 91 |
|
|---|
| [dc033a1] | 92 | /** Information on row-span yet unsent to FB driver. */
|
|---|
| 93 | struct {
|
|---|
| 94 | int row; /**< Row where the span lies. */
|
|---|
| 95 | int col; /**< Leftmost column of the span. */
|
|---|
| 96 | int n; /**< Width of the span. */
|
|---|
| 97 | } fb_pending;
|
|---|
| [d2cc7e1] | 98 |
|
|---|
| 99 | /** Size of cwrite_buf. */
|
|---|
| 100 | #define CWRITE_BUF_SIZE 256
|
|---|
| 101 |
|
|---|
| 102 | /** Buffer for receiving data via the CONSOLE_WRITE call from the client. */
|
|---|
| 103 | static char cwrite_buf[CWRITE_BUF_SIZE];
|
|---|
| [429acb9] | 104 |
|
|---|
| [dc033a1] | 105 | static void fb_putchar(char c, int row, int col);
|
|---|
| 106 |
|
|---|
| [bb51e9a8] | 107 |
|
|---|
| [e87e18f] | 108 | /** Find unused virtual console.
|
|---|
| 109 | *
|
|---|
| 110 | */
|
|---|
| [d32af35] | 111 | static int find_free_connection(void)
|
|---|
| [79460ae] | 112 | {
|
|---|
| [c738d65] | 113 | int i;
|
|---|
| [79460ae] | 114 |
|
|---|
| [c738d65] | 115 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
|---|
| [d32af35] | 116 | if (!connections[i].used)
|
|---|
| [79460ae] | 117 | return i;
|
|---|
| 118 | }
|
|---|
| [d32af35] | 119 | return -1;
|
|---|
| [79460ae] | 120 | }
|
|---|
| 121 |
|
|---|
| [429acb9] | 122 | static void clrscr(void)
|
|---|
| 123 | {
|
|---|
| [0cc4313] | 124 | async_msg_0(fb_info.phone, FB_CLEAR);
|
|---|
| [429acb9] | 125 | }
|
|---|
| 126 |
|
|---|
| [8c6337d] | 127 | static void curs_visibility(bool visible)
|
|---|
| [429acb9] | 128 | {
|
|---|
| [8c6337d] | 129 | async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static void curs_hide_sync(void)
|
|---|
| 133 | {
|
|---|
| 134 | ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
|
|---|
| [429acb9] | 135 | }
|
|---|
| 136 |
|
|---|
| 137 | static void curs_goto(int row, int col)
|
|---|
| 138 | {
|
|---|
| [085bd54] | 139 | async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
|
|---|
| [429acb9] | 140 | }
|
|---|
| 141 |
|
|---|
| [9805cde] | 142 | static void set_style(int style)
|
|---|
| [429acb9] | 143 | {
|
|---|
| [9805cde] | 144 | async_msg_1(fb_info.phone, FB_SET_STYLE, style);
|
|---|
| [429acb9] | 145 | }
|
|---|
| 146 |
|
|---|
| [9805cde] | 147 | static void set_color(int fgcolor, int bgcolor, int flags)
|
|---|
| [429acb9] | 148 | {
|
|---|
| [9805cde] | 149 | async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static void set_rgb_color(int fgcolor, int bgcolor)
|
|---|
| 153 | {
|
|---|
| 154 | async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | static void set_attrs(attrs_t *attrs)
|
|---|
| 158 | {
|
|---|
| 159 | switch (attrs->t) {
|
|---|
| 160 | case at_style:
|
|---|
| 161 | set_style(attrs->a.s.style);
|
|---|
| 162 | break;
|
|---|
| 163 |
|
|---|
| 164 | case at_idx:
|
|---|
| 165 | set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
|
|---|
| 166 | attrs->a.i.flags);
|
|---|
| 167 | break;
|
|---|
| 168 |
|
|---|
| 169 | case at_rgb:
|
|---|
| 170 | set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
|
|---|
| 171 | break;
|
|---|
| 172 | }
|
|---|
| [429acb9] | 173 | }
|
|---|
| 174 |
|
|---|
| [dc033a1] | 175 | /** Send an area of screenbuffer to the FB driver. */
|
|---|
| 176 | static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
|
|---|
| [d2cc7e1] | 177 | {
|
|---|
| [dc033a1] | 178 | int i, j;
|
|---|
| 179 | int rc;
|
|---|
| 180 | attrs_t *attrs;
|
|---|
| 181 | keyfield_t *field;
|
|---|
| [d2cc7e1] | 182 |
|
|---|
| [dc033a1] | 183 | if (interbuffer) {
|
|---|
| 184 | for (j = 0; j < h; j++) {
|
|---|
| 185 | for (i = 0; i < w; i++) {
|
|---|
| 186 | interbuffer[i + j * w] =
|
|---|
| 187 | *get_field_at(&conn->screenbuffer,
|
|---|
| 188 | x + i, y + j);
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| [d2cc7e1] | 191 |
|
|---|
| [dc033a1] | 192 | rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
|
|---|
| 193 | x, y, w, h);
|
|---|
| 194 | } else {
|
|---|
| 195 | rc = ENOTSUP;
|
|---|
| [d2cc7e1] | 196 | }
|
|---|
| 197 |
|
|---|
| [dc033a1] | 198 | if (rc != 0) {
|
|---|
| 199 | attrs = &conn->screenbuffer.attrs;
|
|---|
| [d2cc7e1] | 200 |
|
|---|
| [dc033a1] | 201 | for (j = 0; j < h; j++) {
|
|---|
| 202 | for (i = 0; i < w; i++) {
|
|---|
| 203 | field = get_field_at(&conn->screenbuffer,
|
|---|
| 204 | x + i, y + j);
|
|---|
| 205 | if (!attrs_same(*attrs, field->attrs))
|
|---|
| 206 | set_attrs(&field->attrs);
|
|---|
| 207 | attrs = &field->attrs;
|
|---|
| 208 |
|
|---|
| 209 | fb_putchar(field->character, y + j, x + i);
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| [d2cc7e1] | 213 | }
|
|---|
| 214 |
|
|---|
| [dc033a1] | 215 | /** Flush pending cells to FB. */
|
|---|
| 216 | static void fb_pending_flush(void)
|
|---|
| [d2cc7e1] | 217 | {
|
|---|
| 218 | screenbuffer_t *scr;
|
|---|
| 219 |
|
|---|
| [dc033a1] | 220 | scr = &(connections[active_console].screenbuffer);
|
|---|
| [d2cc7e1] | 221 |
|
|---|
| [dc033a1] | 222 | if (fb_pending.n > 0) {
|
|---|
| 223 | fb_update_area(&connections[active_console], fb_pending.col,
|
|---|
| 224 | fb_pending.row, fb_pending.n, 1);
|
|---|
| 225 | fb_pending.n = 0;
|
|---|
| [d2cc7e1] | 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| [dc033a1] | 229 | /** Mark a character cell as changed.
|
|---|
| 230 | *
|
|---|
| 231 | * This adds the cell to the pending rowspan if possible. Otherwise
|
|---|
| 232 | * the old span is flushed first.
|
|---|
| 233 | */
|
|---|
| 234 | static void cell_mark_changed(int row, int col)
|
|---|
| [429acb9] | 235 | {
|
|---|
| [dc033a1] | 236 | if (fb_pending.n != 0) {
|
|---|
| 237 | if (row != fb_pending.row ||
|
|---|
| 238 | col != fb_pending.col + fb_pending.n) {
|
|---|
| 239 | fb_pending_flush();
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| [d2cc7e1] | 242 |
|
|---|
| [dc033a1] | 243 | if (fb_pending.n == 0) {
|
|---|
| 244 | fb_pending.row = row;
|
|---|
| 245 | fb_pending.col = col;
|
|---|
| [d2cc7e1] | 246 | }
|
|---|
| 247 |
|
|---|
| [dc033a1] | 248 | ++fb_pending.n;
|
|---|
| [429acb9] | 249 | }
|
|---|
| 250 |
|
|---|
| [dc033a1] | 251 |
|
|---|
| 252 | /** Print a character to the active VC with buffering. */
|
|---|
| 253 | static void fb_putchar(char c, int row, int col)
|
|---|
| 254 | {
|
|---|
| 255 | async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /** Process a character from the client (TTY emulation). */
|
|---|
| [10569b1] | 259 | static void write_char(int console, char key)
|
|---|
| 260 | {
|
|---|
| [d2cc7e1] | 261 | bool flush_cursor = false;
|
|---|
| [10569b1] | 262 | screenbuffer_t *scr = &(connections[console].screenbuffer);
|
|---|
| [d2cc7e1] | 263 |
|
|---|
| [10569b1] | 264 | switch (key) {
|
|---|
| [00bb6965] | 265 | case '\n':
|
|---|
| [dc033a1] | 266 | fb_pending_flush();
|
|---|
| [d2cc7e1] | 267 | flush_cursor = true;
|
|---|
| [c738d65] | 268 | scr->position_y++;
|
|---|
| 269 | scr->position_x = 0;
|
|---|
| [00bb6965] | 270 | break;
|
|---|
| 271 | case '\r':
|
|---|
| 272 | break;
|
|---|
| 273 | case '\t':
|
|---|
| 274 | scr->position_x += 8;
|
|---|
| 275 | scr->position_x -= scr->position_x % 8;
|
|---|
| 276 | break;
|
|---|
| 277 | case '\b':
|
|---|
| 278 | if (scr->position_x == 0)
|
|---|
| [10569b1] | 279 | break;
|
|---|
| [00bb6965] | 280 | scr->position_x--;
|
|---|
| 281 | if (console == active_console)
|
|---|
| [dc033a1] | 282 | cell_mark_changed(scr->position_y, scr->position_x);
|
|---|
| [00bb6965] | 283 | screenbuffer_putchar(scr, ' ');
|
|---|
| 284 | break;
|
|---|
| 285 | default:
|
|---|
| 286 | if (console == active_console)
|
|---|
| [dc033a1] | 287 | cell_mark_changed(scr->position_y, scr->position_x);
|
|---|
| [10569b1] | 288 |
|
|---|
| [00bb6965] | 289 | screenbuffer_putchar(scr, key);
|
|---|
| 290 | scr->position_x++;
|
|---|
| [10569b1] | 291 | }
|
|---|
| [d2cc7e1] | 292 |
|
|---|
| 293 | if (scr->position_x >= scr->size_x) {
|
|---|
| 294 | flush_cursor = true;
|
|---|
| 295 | scr->position_y++;
|
|---|
| 296 | }
|
|---|
| [10569b1] | 297 |
|
|---|
| 298 | if (scr->position_y >= scr->size_y) {
|
|---|
| [dc033a1] | 299 | fb_pending_flush();
|
|---|
| [10569b1] | 300 | scr->position_y = scr->size_y - 1;
|
|---|
| [c891ed39] | 301 | screenbuffer_clear_line(scr, scr->top_line);
|
|---|
| [c738d65] | 302 | scr->top_line = (scr->top_line + 1) % scr->size_y;
|
|---|
| [b1f51f0] | 303 | if (console == active_console)
|
|---|
| [0cc4313] | 304 | async_msg_1(fb_info.phone, FB_SCROLL, 1);
|
|---|
| [10569b1] | 305 | }
|
|---|
| [d2cc7e1] | 306 |
|
|---|
| [10569b1] | 307 | scr->position_x = scr->position_x % scr->size_x;
|
|---|
| [d2cc7e1] | 308 |
|
|---|
| 309 | if (console == active_console && flush_cursor)
|
|---|
| [429acb9] | 310 | curs_goto(scr->position_y, scr->position_x);
|
|---|
| [10569b1] | 311 | }
|
|---|
| 312 |
|
|---|
| [429acb9] | 313 | /** Switch to new console */
|
|---|
| 314 | static void change_console(int newcons)
|
|---|
| 315 | {
|
|---|
| 316 | connection_t *conn;
|
|---|
| [bd02038] | 317 | int i, j, rc;
|
|---|
| [6d5005c] | 318 | keyfield_t *field;
|
|---|
| [9805cde] | 319 | attrs_t *attrs;
|
|---|
| [76fca31] | 320 |
|
|---|
| [429acb9] | 321 | if (newcons == active_console)
|
|---|
| 322 | return;
|
|---|
| [d2cc7e1] | 323 |
|
|---|
| [dc033a1] | 324 | fb_pending_flush();
|
|---|
| [d2cc7e1] | 325 |
|
|---|
| [a7d2d78] | 326 | if (newcons == KERNEL_CONSOLE) {
|
|---|
| [1f83244] | 327 | async_serialize_start();
|
|---|
| [8c6337d] | 328 | curs_hide_sync();
|
|---|
| [76fca31] | 329 | gcons_in_kernel();
|
|---|
| [1f83244] | 330 | async_serialize_end();
|
|---|
| [76fca31] | 331 |
|
|---|
| [3ad953c] | 332 | if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
|
|---|
| 333 | prev_console = active_console;
|
|---|
| [76fca31] | 334 | active_console = KERNEL_CONSOLE;
|
|---|
| [3ad953c] | 335 | } else
|
|---|
| [8c6337d] | 336 | newcons = active_console;
|
|---|
| [01f5e17] | 337 | }
|
|---|
| [6d5005c] | 338 |
|
|---|
| [76fca31] | 339 | if (newcons != KERNEL_CONSOLE) {
|
|---|
| 340 | async_serialize_start();
|
|---|
| 341 |
|
|---|
| 342 | if (active_console == KERNEL_CONSOLE)
|
|---|
| 343 | gcons_redraw_console();
|
|---|
| 344 |
|
|---|
| 345 | active_console = newcons;
|
|---|
| 346 | gcons_change_console(newcons);
|
|---|
| 347 | conn = &connections[active_console];
|
|---|
| 348 |
|
|---|
| [9805cde] | 349 | set_attrs(&conn->screenbuffer.attrs);
|
|---|
| [8c6337d] | 350 | curs_visibility(false);
|
|---|
| [76fca31] | 351 | if (interbuffer) {
|
|---|
| [dc033a1] | 352 | for (j = 0; j < conn->screenbuffer.size_y; j++) {
|
|---|
| 353 | for (i = 0; i < conn->screenbuffer.size_x; i++) {
|
|---|
| [76fca31] | 354 | unsigned int size_x;
|
|---|
| 355 |
|
|---|
| 356 | size_x = conn->screenbuffer.size_x;
|
|---|
| [dc033a1] | 357 | interbuffer[j * size_x + i] =
|
|---|
| [76fca31] | 358 | *get_field_at(&conn->screenbuffer, i, j);
|
|---|
| 359 | }
|
|---|
| [dc033a1] | 360 | }
|
|---|
| [76fca31] | 361 | /* This call can preempt, but we are already at the end */
|
|---|
| [dc033a1] | 362 | rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
|
|---|
| 363 | 0, 0, conn->screenbuffer.size_x,
|
|---|
| 364 | conn->screenbuffer.size_y);
|
|---|
| [76fca31] | 365 | }
|
|---|
| 366 |
|
|---|
| 367 | if ((!interbuffer) || (rc != 0)) {
|
|---|
| [9805cde] | 368 | set_attrs(&conn->screenbuffer.attrs);
|
|---|
| [76fca31] | 369 | clrscr();
|
|---|
| [9805cde] | 370 | attrs = &conn->screenbuffer.attrs;
|
|---|
| [76fca31] | 371 |
|
|---|
| [3ad953c] | 372 | for (j = 0; j < conn->screenbuffer.size_y; j++)
|
|---|
| [76fca31] | 373 | for (i = 0; i < conn->screenbuffer.size_x; i++) {
|
|---|
| 374 | field = get_field_at(&conn->screenbuffer, i, j);
|
|---|
| [9805cde] | 375 | if (!attrs_same(*attrs, field->attrs))
|
|---|
| 376 | set_attrs(&field->attrs);
|
|---|
| 377 | attrs = &field->attrs;
|
|---|
| [76fca31] | 378 | if ((field->character == ' ') &&
|
|---|
| [9805cde] | 379 | (attrs_same(field->attrs,
|
|---|
| 380 | conn->screenbuffer.attrs)))
|
|---|
| [76fca31] | 381 | continue;
|
|---|
| [9805cde] | 382 |
|
|---|
| [dc033a1] | 383 | fb_putchar(field->character, j, i);
|
|---|
| [76fca31] | 384 | }
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | curs_goto(conn->screenbuffer.position_y,
|
|---|
| 388 | conn->screenbuffer.position_x);
|
|---|
| 389 | curs_visibility(conn->screenbuffer.is_cursor_visible);
|
|---|
| 390 |
|
|---|
| 391 | async_serialize_end();
|
|---|
| [429acb9] | 392 | }
|
|---|
| 393 | }
|
|---|
| [10569b1] | 394 |
|
|---|
| [e87e18f] | 395 | /** Handler for keyboard */
|
|---|
| [eaf34f7] | 396 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| [51c1b003] | 397 | {
|
|---|
| [eaf34f7] | 398 | ipc_callid_t callid;
|
|---|
| [51c1b003] | 399 | ipc_call_t call;
|
|---|
| [eaf34f7] | 400 | int retval;
|
|---|
| [fa09449] | 401 | kbd_event_t ev;
|
|---|
| [c1d2c9d] | 402 | connection_t *conn;
|
|---|
| [1f83244] | 403 | int newcon;
|
|---|
| [bb51e9a8] | 404 |
|
|---|
| [eaf34f7] | 405 | /* Ignore parameters, the connection is alread opened */
|
|---|
| 406 | while (1) {
|
|---|
| 407 | callid = async_get_call(&call);
|
|---|
| 408 | switch (IPC_GET_METHOD(call)) {
|
|---|
| 409 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 410 | /* TODO: Handle hangup */
|
|---|
| 411 | return;
|
|---|
| [1f83244] | 412 | case KBD_MS_LEFT:
|
|---|
| 413 | newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
|
|---|
| 414 | if (newcon != -1)
|
|---|
| 415 | change_console(newcon);
|
|---|
| [501a8ba] | 416 | retval = 0;
|
|---|
| [1f83244] | 417 | break;
|
|---|
| [830ac99] | 418 | case KBD_MS_MOVE:
|
|---|
| [00bb6965] | 419 | gcons_mouse_move(IPC_GET_ARG1(call),
|
|---|
| [01f5e17] | 420 | IPC_GET_ARG2(call));
|
|---|
| [501a8ba] | 421 | retval = 0;
|
|---|
| [830ac99] | 422 | break;
|
|---|
| [fa09449] | 423 | case KBD_EVENT:
|
|---|
| 424 | /* Got event from keyboard driver. */
|
|---|
| [eaf34f7] | 425 | retval = 0;
|
|---|
| [fa09449] | 426 | ev.type = IPC_GET_ARG1(call);
|
|---|
| 427 | ev.key = IPC_GET_ARG2(call);
|
|---|
| 428 | ev.mods = IPC_GET_ARG3(call);
|
|---|
| 429 | ev.c = IPC_GET_ARG4(call);
|
|---|
| 430 |
|
|---|
| [eaf34f7] | 431 | /* switch to another virtual console */
|
|---|
| [cf28036c] | 432 |
|
|---|
| [c1d2c9d] | 433 | conn = &connections[active_console];
|
|---|
| [fa09449] | 434 |
|
|---|
| [f89979b] | 435 | if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
|
|---|
| [fa09449] | 436 | CONSOLE_COUNT)) {
|
|---|
| [f89979b] | 437 | if (ev.key == KC_F12)
|
|---|
| [a7d2d78] | 438 | change_console(KERNEL_CONSOLE);
|
|---|
| [429acb9] | 439 | else
|
|---|
| [f89979b] | 440 | change_console(ev.key - KC_F1);
|
|---|
| [eaf34f7] | 441 | break;
|
|---|
| 442 | }
|
|---|
| [cf28036c] | 443 |
|
|---|
| 444 | /* if client is awaiting key, send it */
|
|---|
| [c1d2c9d] | 445 | if (conn->keyrequest_counter > 0) {
|
|---|
| 446 | conn->keyrequest_counter--;
|
|---|
| [fa09449] | 447 | ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
|
|---|
| 448 | ev.type, ev.key, ev.mods, ev.c);
|
|---|
| [cf28036c] | 449 | break;
|
|---|
| 450 | }
|
|---|
| [fa09449] | 451 |
|
|---|
| 452 | keybuffer_push(&conn->keybuffer, &ev);
|
|---|
| [501a8ba] | 453 | retval = 0;
|
|---|
| [fa09449] | 454 |
|
|---|
| [eaf34f7] | 455 | break;
|
|---|
| 456 | default:
|
|---|
| [1029d3d3] | 457 | retval = ENOENT;
|
|---|
| [085bd54] | 458 | }
|
|---|
| [b74959bd] | 459 | ipc_answer_0(callid, retval);
|
|---|
| [eaf34f7] | 460 | }
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| [d2cc7e1] | 463 | /** Handle CONSOLE_WRITE call. */
|
|---|
| 464 | static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 465 | {
|
|---|
| 466 | ipc_callid_t callid;
|
|---|
| 467 | size_t len;
|
|---|
| 468 | size_t i;
|
|---|
| 469 |
|
|---|
| 470 | if (!ipc_data_write_receive(&callid, &len)) {
|
|---|
| 471 | ipc_answer_0(callid, EINVAL);
|
|---|
| 472 | ipc_answer_0(rid, EINVAL);
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | if (len > CWRITE_BUF_SIZE)
|
|---|
| 476 | len = CWRITE_BUF_SIZE;
|
|---|
| 477 |
|
|---|
| 478 | (void) ipc_data_write_finalize(callid, cwrite_buf, len);
|
|---|
| 479 |
|
|---|
| 480 | for (i = 0; i < len; i++) {
|
|---|
| 481 | write_char(consnum, cwrite_buf[i]);
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | gcons_notify_char(consnum);
|
|---|
| 485 | ipc_answer_1(rid, EOK, len);
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| [eaf34f7] | 488 | /** Default thread for new connections */
|
|---|
| [b1f51f0] | 489 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| [eaf34f7] | 490 | {
|
|---|
| [51c1b003] | 491 | ipc_callid_t callid;
|
|---|
| [eaf34f7] | 492 | ipc_call_t call;
|
|---|
| 493 | int consnum;
|
|---|
| [fa09449] | 494 | ipcarg_t arg1, arg2, arg3, arg4;
|
|---|
| [e9073f2] | 495 | connection_t *conn;
|
|---|
| [dc033a1] | 496 | screenbuffer_t *scr;
|
|---|
| [3ad953c] | 497 |
|
|---|
| [d32af35] | 498 | if ((consnum = find_free_connection()) == -1) {
|
|---|
| [b74959bd] | 499 | ipc_answer_0(iid, ELIMIT);
|
|---|
| [eaf34f7] | 500 | return;
|
|---|
| 501 | }
|
|---|
| [e9073f2] | 502 | conn = &connections[consnum];
|
|---|
| [085bd54] | 503 | conn->used = 1;
|
|---|
| [a7d2d78] | 504 |
|
|---|
| [085bd54] | 505 | async_serialize_start();
|
|---|
| [a7d2d78] | 506 | gcons_notify_connect(consnum);
|
|---|
| [38c706cc] | 507 | conn->client_phone = IPC_GET_ARG5(*icall);
|
|---|
| [e9073f2] | 508 | screenbuffer_clear(&conn->screenbuffer);
|
|---|
| [10569b1] | 509 |
|
|---|
| [eaf34f7] | 510 | /* Accept the connection */
|
|---|
| [b74959bd] | 511 | ipc_answer_0(iid, EOK);
|
|---|
| [3ad953c] | 512 |
|
|---|
| [eaf34f7] | 513 | while (1) {
|
|---|
| [085bd54] | 514 | async_serialize_end();
|
|---|
| [eaf34f7] | 515 | callid = async_get_call(&call);
|
|---|
| [085bd54] | 516 | async_serialize_start();
|
|---|
| [3ad953c] | 517 |
|
|---|
| [96858e8] | 518 | arg1 = 0;
|
|---|
| 519 | arg2 = 0;
|
|---|
| [fa09449] | 520 | arg3 = 0;
|
|---|
| 521 | arg4 = 0;
|
|---|
| 522 |
|
|---|
| [eaf34f7] | 523 | switch (IPC_GET_METHOD(call)) {
|
|---|
| 524 | case IPC_M_PHONE_HUNGUP:
|
|---|
| [e9073f2] | 525 | gcons_notify_disconnect(consnum);
|
|---|
| [085bd54] | 526 |
|
|---|
| [e9073f2] | 527 | /* Answer all pending requests */
|
|---|
| [3ad953c] | 528 | while (conn->keyrequest_counter > 0) {
|
|---|
| [e9073f2] | 529 | conn->keyrequest_counter--;
|
|---|
| [b74959bd] | 530 | ipc_answer_0(fifo_pop(conn->keyrequests),
|
|---|
| 531 | ENOENT);
|
|---|
| [e9073f2] | 532 | break;
|
|---|
| 533 | }
|
|---|
| [0d11fc1c] | 534 | conn->used = 0;
|
|---|
| [eaf34f7] | 535 | return;
|
|---|
| 536 | case CONSOLE_PUTCHAR:
|
|---|
| [10569b1] | 537 | write_char(consnum, IPC_GET_ARG1(call));
|
|---|
| [d6cc453] | 538 | gcons_notify_char(consnum);
|
|---|
| [ad123964] | 539 | break;
|
|---|
| [d2cc7e1] | 540 | case CONSOLE_WRITE:
|
|---|
| 541 | cons_write(consnum, callid, &call);
|
|---|
| 542 | continue;
|
|---|
| [ad123964] | 543 | case CONSOLE_CLEAR:
|
|---|
| [3993b3d] | 544 | /* Send message to fb */
|
|---|
| 545 | if (consnum == active_console) {
|
|---|
| [0cc4313] | 546 | async_msg_0(fb_info.phone, FB_CLEAR);
|
|---|
| [3993b3d] | 547 | }
|
|---|
| 548 |
|
|---|
| [e9073f2] | 549 | screenbuffer_clear(&conn->screenbuffer);
|
|---|
| [3993b3d] | 550 |
|
|---|
| [eaf34f7] | 551 | break;
|
|---|
| [ad123964] | 552 | case CONSOLE_GOTO:
|
|---|
| [00bb6965] | 553 | screenbuffer_goto(&conn->screenbuffer,
|
|---|
| [01f5e17] | 554 | IPC_GET_ARG2(call), IPC_GET_ARG1(call));
|
|---|
| [6118e5f6] | 555 | if (consnum == active_console)
|
|---|
| [00bb6965] | 556 | curs_goto(IPC_GET_ARG1(call),
|
|---|
| [01f5e17] | 557 | IPC_GET_ARG2(call));
|
|---|
| [ad123964] | 558 | break;
|
|---|
| [3756912] | 559 | case CONSOLE_GETSIZE:
|
|---|
| [d6cc453] | 560 | arg1 = fb_info.rows;
|
|---|
| 561 | arg2 = fb_info.cols;
|
|---|
| [3756912] | 562 | break;
|
|---|
| [0c6984e] | 563 | case CONSOLE_FLUSH:
|
|---|
| [dc033a1] | 564 | fb_pending_flush();
|
|---|
| 565 | if (consnum == active_console) {
|
|---|
| [0cc4313] | 566 | async_req_0_0(fb_info.phone, FB_FLUSH);
|
|---|
| [dc033a1] | 567 |
|
|---|
| 568 | scr = &(connections[consnum].screenbuffer);
|
|---|
| 569 | curs_goto(scr->position_y, scr->position_x);
|
|---|
| 570 | }
|
|---|
| [a9bd960c] | 571 | break;
|
|---|
| 572 | case CONSOLE_SET_STYLE:
|
|---|
| [dc033a1] | 573 | fb_pending_flush();
|
|---|
| [9805cde] | 574 | arg1 = IPC_GET_ARG1(call);
|
|---|
| 575 | screenbuffer_set_style(&conn->screenbuffer, arg1);
|
|---|
| 576 | if (consnum == active_console)
|
|---|
| 577 | set_style(arg1);
|
|---|
| 578 | break;
|
|---|
| 579 | case CONSOLE_SET_COLOR:
|
|---|
| [dc033a1] | 580 | fb_pending_flush();
|
|---|
| [9805cde] | 581 | arg1 = IPC_GET_ARG1(call);
|
|---|
| 582 | arg2 = IPC_GET_ARG2(call);
|
|---|
| 583 | arg3 = IPC_GET_ARG3(call);
|
|---|
| 584 | screenbuffer_set_color(&conn->screenbuffer, arg1,
|
|---|
| 585 | arg2, arg3);
|
|---|
| 586 | if (consnum == active_console)
|
|---|
| 587 | set_color(arg1, arg2, arg3);
|
|---|
| 588 | break;
|
|---|
| 589 | case CONSOLE_SET_RGB_COLOR:
|
|---|
| [dc033a1] | 590 | fb_pending_flush();
|
|---|
| [a9bd960c] | 591 | arg1 = IPC_GET_ARG1(call);
|
|---|
| 592 | arg2 = IPC_GET_ARG2(call);
|
|---|
| [9805cde] | 593 | screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
|
|---|
| [01f5e17] | 594 | arg2);
|
|---|
| [a9bd960c] | 595 | if (consnum == active_console)
|
|---|
| [9805cde] | 596 | set_rgb_color(arg1, arg2);
|
|---|
| [0c6984e] | 597 | break;
|
|---|
| [a8b2b5b2] | 598 | case CONSOLE_CURSOR_VISIBILITY:
|
|---|
| [dc033a1] | 599 | fb_pending_flush();
|
|---|
| [a8b2b5b2] | 600 | arg1 = IPC_GET_ARG1(call);
|
|---|
| [e9073f2] | 601 | conn->screenbuffer.is_cursor_visible = arg1;
|
|---|
| [a8b2b5b2] | 602 | if (consnum == active_console)
|
|---|
| 603 | curs_visibility(arg1);
|
|---|
| 604 | break;
|
|---|
| [fa09449] | 605 | case CONSOLE_GETKEY:
|
|---|
| [e9073f2] | 606 | if (keybuffer_empty(&conn->keybuffer)) {
|
|---|
| [cf28036c] | 607 | /* buffer is empty -> store request */
|
|---|
| [00bb6965] | 608 | if (conn->keyrequest_counter <
|
|---|
| 609 | MAX_KEYREQUESTS_BUFFERED) {
|
|---|
| [e9073f2] | 610 | fifo_push(conn->keyrequests, callid);
|
|---|
| 611 | conn->keyrequest_counter++;
|
|---|
| [cf28036c] | 612 | } else {
|
|---|
| [00bb6965] | 613 | /*
|
|---|
| 614 | * No key available and too many
|
|---|
| 615 | * requests => fail.
|
|---|
| 616 | */
|
|---|
| [b74959bd] | 617 | ipc_answer_0(callid, ELIMIT);
|
|---|
| [cf28036c] | 618 | }
|
|---|
| 619 | continue;
|
|---|
| [00bb6965] | 620 | }
|
|---|
| [fa09449] | 621 | kbd_event_t ev;
|
|---|
| 622 | keybuffer_pop(&conn->keybuffer, &ev);
|
|---|
| 623 | arg1 = ev.type;
|
|---|
| 624 | arg2 = ev.key;
|
|---|
| 625 | arg3 = ev.mods;
|
|---|
| 626 | arg4 = ev.c;
|
|---|
| [eaf34f7] | 627 | break;
|
|---|
| [3fe00ee] | 628 | case CONSOLE_KCON_ENABLE:
|
|---|
| 629 | change_console(KERNEL_CONSOLE);
|
|---|
| 630 | break;
|
|---|
| [eaf34f7] | 631 | }
|
|---|
| [fa09449] | 632 | ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
|
|---|
| [eaf34f7] | 633 | }
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| [3ad953c] | 636 | static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 637 | {
|
|---|
| 638 | change_console(prev_console);
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| [eaf34f7] | 641 | int main(int argc, char *argv[])
|
|---|
| 642 | {
|
|---|
| [271b540] | 643 | printf(NAME ": HelenOS Console service\n");
|
|---|
| 644 |
|
|---|
| [eaf34f7] | 645 | ipcarg_t phonehash;
|
|---|
| [501a8ba] | 646 | int kbd_phone;
|
|---|
| [7447572] | 647 | size_t ib_size;
|
|---|
| [79460ae] | 648 | int i;
|
|---|
| [3ad953c] | 649 |
|
|---|
| [da0c91e7] | 650 | async_set_client_connection(client_connection);
|
|---|
| [51c1b003] | 651 |
|
|---|
| 652 | /* Connect to keyboard driver */
|
|---|
| [4904de8] | 653 | kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
|
|---|
| 654 | if (kbd_phone < 0) {
|
|---|
| 655 | printf(NAME ": Failed to connect to keyboard service\n");
|
|---|
| 656 | return -1;
|
|---|
| [00bb6965] | 657 | }
|
|---|
| [51c1b003] | 658 |
|
|---|
| [4904de8] | 659 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
|
|---|
| 660 | printf(NAME ": Failed to create callback from keyboard service\n");
|
|---|
| [51c1b003] | 661 | return -1;
|
|---|
| [4904de8] | 662 | }
|
|---|
| 663 |
|
|---|
| [290c0db] | 664 | async_new_connection(phonehash, 0, NULL, keyboard_events);
|
|---|
| 665 |
|
|---|
| [51c1b003] | 666 | /* Connect to framebuffer driver */
|
|---|
| [4904de8] | 667 | fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
|
|---|
| 668 | if (fb_info.phone < 0) {
|
|---|
| 669 | printf(NAME ": Failed to connect to video service\n");
|
|---|
| 670 | return -1;
|
|---|
| [3993b3d] | 671 | }
|
|---|
| [429acb9] | 672 |
|
|---|
| [516ff92] | 673 | /* Disable kernel output to the console */
|
|---|
| 674 | __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
|
|---|
| 675 |
|
|---|
| [e1c4849] | 676 | /* Initialize gcons */
|
|---|
| 677 | gcons_init(fb_info.phone);
|
|---|
| 678 | /* Synchronize, the gcons can have something in queue */
|
|---|
| [0cc4313] | 679 | async_req_0_0(fb_info.phone, FB_FLUSH);
|
|---|
| [3993b3d] | 680 |
|
|---|
| [0cc4313] | 681 | async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
|
|---|
| [01f5e17] | 682 | &fb_info.cols);
|
|---|
| [9805cde] | 683 | set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
|
|---|
| [429acb9] | 684 | clrscr();
|
|---|
| [3993b3d] | 685 |
|
|---|
| 686 | /* Init virtual consoles */
|
|---|
| [79460ae] | 687 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
|---|
| 688 | connections[i].used = 0;
|
|---|
| [01f5e17] | 689 | keybuffer_init(&connections[i].keybuffer);
|
|---|
| [cf28036c] | 690 |
|
|---|
| [01f5e17] | 691 | connections[i].keyrequests.head = 0;
|
|---|
| 692 | connections[i].keyrequests.tail = 0;
|
|---|
| [cf28036c] | 693 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
|
|---|
| 694 | connections[i].keyrequest_counter = 0;
|
|---|
| [3993b3d] | 695 |
|
|---|
| [01f5e17] | 696 | if (screenbuffer_init(&connections[i].screenbuffer,
|
|---|
| 697 | fb_info.cols, fb_info.rows) == NULL) {
|
|---|
| [c738d65] | 698 | /* FIXME: handle error */
|
|---|
| [3993b3d] | 699 | return -1;
|
|---|
| 700 | }
|
|---|
| [79460ae] | 701 | }
|
|---|
| [d32af35] | 702 | connections[KERNEL_CONSOLE].used = 1;
|
|---|
| [7447572] | 703 |
|
|---|
| 704 | /* Set up shared memory buffer. */
|
|---|
| 705 | ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
|
|---|
| 706 | interbuffer = as_get_mappable_page(ib_size);
|
|---|
| 707 |
|
|---|
| [dc033a1] | 708 | fb_pending.n = 0;
|
|---|
| 709 |
|
|---|
| [7447572] | 710 | if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
|
|---|
| 711 | AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
|
|---|
| 712 | interbuffer = NULL;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | if (interbuffer) {
|
|---|
| [215e375] | 716 | if (ipc_share_out_start(fb_info.phone, interbuffer,
|
|---|
| [27d293a] | 717 | AS_AREA_READ) != EOK) {
|
|---|
| [7447572] | 718 | as_area_destroy(interbuffer);
|
|---|
| [390a678] | 719 | interbuffer = NULL;
|
|---|
| 720 | }
|
|---|
| 721 | }
|
|---|
| [3ad953c] | 722 |
|
|---|
| [c738d65] | 723 | curs_goto(0, 0);
|
|---|
| [01f5e17] | 724 | curs_visibility(
|
|---|
| 725 | connections[active_console].screenbuffer.is_cursor_visible);
|
|---|
| [3ad953c] | 726 |
|
|---|
| [b1f51f0] | 727 | /* Register at NS */
|
|---|
| [271b540] | 728 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
|
|---|
| [51c1b003] | 729 | return -1;
|
|---|
| 730 |
|
|---|
| [3ad953c] | 731 | /* Receive kernel notifications */
|
|---|
| [84afc7b] | 732 | // if (sysinfo_value("kconsole.present")) {
|
|---|
| 733 | // int inr = sysinfo_value("kconsole.inr");
|
|---|
| 734 | // if (ipc_register_irq(inr, device_assign_devno(), 0, NULL) != EOK)
|
|---|
| 735 | // printf(NAME ": Error registering kconsole notifications\n");
|
|---|
| 736 | //
|
|---|
| 737 | // async_set_interrupt_received(interrupt_received);
|
|---|
| 738 | // }
|
|---|
| [3ad953c] | 739 |
|
|---|
| [271b540] | 740 | // FIXME: avoid connectiong to itself, keep using klog
|
|---|
| 741 | // printf(NAME ": Accepting connections\n");
|
|---|
| [eaf34f7] | 742 | async_manager();
|
|---|
| [3ad953c] | 743 |
|
|---|
| 744 | return 0;
|
|---|
| [51c1b003] | 745 | }
|
|---|
| [516ff92] | 746 |
|
|---|
| [ce5bcb4] | 747 | /** @}
|
|---|
| 748 | */
|
|---|