source: mainline/uspace/srv/hid/console/console.c@ a40dea3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a40dea3 was a40dea3, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Eliminate devmap_obsolete API.

  • Property mode set to 100644
File size: 20.8 KB
RevLine 
[51c1b003]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[854eddd6]3 * Copyright (c) 2011 Jiri Svoboda
[51c1b003]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
[ce5bcb4]29
[231a60a]30/** @addtogroup console
[1601f3c]31 * @{
[ce5bcb4]32 */
33/** @file
34 */
35
[3209923]36#include <libc.h>
[5f88293]37#include <ipc/input.h>
[424cd43]38#include <io/keycode.h>
[79460ae]39#include <ipc/fb.h>
[51c1b003]40#include <ipc/services.h>
[79ae36dd]41#include <ns.h>
42#include <ns_obsolete.h>
[51c1b003]43#include <errno.h>
[30db06c]44#include <str_error.h>
[d3e6935]45#include <ipc/console.h>
[eaf34f7]46#include <unistd.h>
47#include <async.h>
[79ae36dd]48#include <async_obsolete.h>
[d9c8c81]49#include <adt/fifo.h>
[2def788]50#include <sys/mman.h>
[271b540]51#include <stdio.h>
[19f857a]52#include <str.h>
[3ad953c]53#include <sysinfo.h>
[05641a9e]54#include <event.h>
[424cd43]55#include <devmap.h>
[47a350f]56#include <fcntl.h>
57#include <vfs/vfs.h>
[1e4cada]58#include <fibril_synch.h>
[369a5f8]59#include <io/style.h>
60#include <io/screenbuffer.h>
[79460ae]61
[9805cde]62#include "console.h"
[e1c4849]63#include "gcons.h"
[cc1f8d4]64#include "keybuffer.h"
[369a5f8]65
[1313ee9]66#define NAME "console"
67#define NAMESPACE "term"
[79ae36dd]68
[a40dea3]69/** Session with the input server. */
70static async_sess_t *input_sess;
[9f51afc]71
[dc033a1]72/** Information about framebuffer */
[3993b3d]73struct {
[9f1362d4]74 int phone; /**< Framebuffer phone */
[96b02eb9]75 sysarg_t cols; /**< Framebuffer columns */
76 sysarg_t rows; /**< Framebuffer rows */
77 sysarg_t color_cap; /**< Color capabilities (FB_CCAP_xxx) */
[3993b3d]78} fb_info;
79
[79460ae]80typedef struct {
[424cd43]81 size_t index; /**< Console index */
82 size_t refcount; /**< Connection reference count */
[991f645]83 devmap_handle_t devmap_handle; /**< Device handle */
[424cd43]84 keybuffer_t keybuffer; /**< Buffer for incoming keys. */
85 screenbuffer_t scr; /**< Screenbuffer for saving screen
86 contents and related settings. */
87} console_t;
[79460ae]88
[1601f3c]89/** Array of data for virtual consoles */
[424cd43]90static console_t consoles[CONSOLE_COUNT];
91
92static console_t *active_console = &consoles[0];
93static console_t *prev_console = &consoles[0];
94static console_t *kernel_console = &consoles[KERNEL_CONSOLE];
[1601f3c]95
96/** Pointer to memory shared with framebufer used for
97 faster virtual console switching */
98static keyfield_t *interbuffer = NULL;
[d2cc7e1]99
[dc033a1]100/** Information on row-span yet unsent to FB driver. */
101struct {
[96b02eb9]102 sysarg_t col; /**< Leftmost column of the span. */
103 sysarg_t row; /**< Row where the span lies. */
104 sysarg_t cnt; /**< Width of the span. */
[dc033a1]105} fb_pending;
[d2cc7e1]106
[953769f]107static FIBRIL_MUTEX_INITIALIZE(input_mutex);
108static FIBRIL_CONDVAR_INITIALIZE(input_cv);
[429acb9]109
[a40dea3]110static FIBRIL_MUTEX_INITIALIZE(big_console_lock);
111
112static void console_serialize_start(void)
113{
114 fibril_mutex_lock(&big_console_lock);
115}
116
117static void console_serialize_end(void)
118{
119 fibril_mutex_unlock(&big_console_lock);
120}
121
[8c6337d]122static void curs_visibility(bool visible)
[429acb9]123{
[79ae36dd]124 async_obsolete_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
[8c6337d]125}
126
127static void curs_hide_sync(void)
128{
[79ae36dd]129 async_obsolete_req_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
[429acb9]130}
131
[96b02eb9]132static void curs_goto(sysarg_t x, sysarg_t y)
[429acb9]133{
[79ae36dd]134 async_obsolete_msg_2(fb_info.phone, FB_CURSOR_GOTO, x, y);
[424cd43]135}
136
137static void screen_clear(void)
138{
[79ae36dd]139 async_obsolete_msg_0(fb_info.phone, FB_CLEAR);
[429acb9]140}
141
[ebfabf6]142static void screen_yield(void)
[10270a8]143{
[79ae36dd]144 async_obsolete_req_0_0(fb_info.phone, FB_SCREEN_YIELD);
[10270a8]145}
146
[ebfabf6]147static void screen_reclaim(void)
[10270a8]148{
[79ae36dd]149 async_obsolete_req_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
[10270a8]150}
151
[854eddd6]152static void input_yield(void)
[ccd1a14]153{
[a40dea3]154 async_exch_t *exch = async_exchange_begin(input_sess);
155 if (exch == NULL) {
156 printf("%s: Failed starting exchange with input device.\n",
157 NAME);
158 return;
159 }
160
161 async_req_0_0(exch, INPUT_YIELD);
162 async_exchange_end(exch);
[ccd1a14]163}
164
[854eddd6]165static void input_reclaim(void)
[ccd1a14]166{
[a40dea3]167 async_exch_t *exch = async_exchange_begin(input_sess);
168 if (exch == NULL) {
169 printf("%s: Failed starting exchange with input device.\n",
170 NAME);
171 return;
172 }
173
174 async_req_0_0(exch, INPUT_RECLAIM);
175 async_exchange_end(exch);
[ccd1a14]176}
177
[9f1362d4]178static void set_style(uint8_t style)
[429acb9]179{
[79ae36dd]180 async_obsolete_msg_1(fb_info.phone, FB_SET_STYLE, style);
[429acb9]181}
182
[9f1362d4]183static void set_color(uint8_t fgcolor, uint8_t bgcolor, uint8_t flags)
[429acb9]184{
[79ae36dd]185 async_obsolete_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
[9805cde]186}
187
[9f1362d4]188static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor)
[9805cde]189{
[79ae36dd]190 async_obsolete_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
[9805cde]191}
192
193static void set_attrs(attrs_t *attrs)
194{
195 switch (attrs->t) {
196 case at_style:
197 set_style(attrs->a.s.style);
198 break;
199 case at_idx:
200 set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
201 attrs->a.i.flags);
202 break;
203 case at_rgb:
204 set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
205 break;
206 }
[429acb9]207}
208
[96b02eb9]209static int ccap_fb_to_con(sysarg_t ccap_fb, sysarg_t *ccap_con)
[50cfa6c]210{
211 switch (ccap_fb) {
[9f1362d4]212 case FB_CCAP_NONE:
213 *ccap_con = CONSOLE_CCAP_NONE;
214 break;
215 case FB_CCAP_STYLE:
216 *ccap_con = CONSOLE_CCAP_STYLE;
217 break;
218 case FB_CCAP_INDEXED:
219 *ccap_con = CONSOLE_CCAP_INDEXED;
220 break;
221 case FB_CCAP_RGB:
222 *ccap_con = CONSOLE_CCAP_RGB;
223 break;
224 default:
225 return EINVAL;
[50cfa6c]226 }
[9f1362d4]227
[50cfa6c]228 return EOK;
229}
230
[dc033a1]231/** Send an area of screenbuffer to the FB driver. */
[96b02eb9]232static void fb_update_area(console_t *cons, sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height)
[d2cc7e1]233{
[dc033a1]234 if (interbuffer) {
[96b02eb9]235 sysarg_t x;
236 sysarg_t y;
[424cd43]237
238 for (y = 0; y < height; y++) {
239 for (x = 0; x < width; x++) {
240 interbuffer[y * width + x] =
241 *get_field_at(&cons->scr, x0 + x, y0 + y);
[dc033a1]242 }
243 }
[1601f3c]244
[79ae36dd]245 async_obsolete_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]246 x0, y0, width, height);
[dc033a1]247 }
[d2cc7e1]248}
249
[dc033a1]250/** Flush pending cells to FB. */
251static void fb_pending_flush(void)
[d2cc7e1]252{
[1601f3c]253 if (fb_pending.cnt > 0) {
[424cd43]254 fb_update_area(active_console, fb_pending.col,
[1601f3c]255 fb_pending.row, fb_pending.cnt, 1);
256 fb_pending.cnt = 0;
[d2cc7e1]257 }
258}
259
[dc033a1]260/** Mark a character cell as changed.
261 *
262 * This adds the cell to the pending rowspan if possible. Otherwise
263 * the old span is flushed first.
[1601f3c]264 *
[dc033a1]265 */
[96b02eb9]266static void cell_mark_changed(sysarg_t col, sysarg_t row)
[429acb9]267{
[1601f3c]268 if (fb_pending.cnt != 0) {
[424cd43]269 if ((col != fb_pending.col + fb_pending.cnt)
270 || (row != fb_pending.row)) {
[dc033a1]271 fb_pending_flush();
272 }
273 }
[1601f3c]274
275 if (fb_pending.cnt == 0) {
[dc033a1]276 fb_pending.col = col;
[424cd43]277 fb_pending.row = row;
[d2cc7e1]278 }
[1601f3c]279
280 fb_pending.cnt++;
[429acb9]281}
282
[dc033a1]283/** Print a character to the active VC with buffering. */
[96b02eb9]284static void fb_putchar(wchar_t c, sysarg_t col, sysarg_t row)
[dc033a1]285{
[79ae36dd]286 async_obsolete_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
[dc033a1]287}
288
289/** Process a character from the client (TTY emulation). */
[424cd43]290static void write_char(console_t *cons, wchar_t ch)
[10569b1]291{
[0ed2e0e]292 bool flush_cursor = false;
[9f1362d4]293
[7ce3cb2]294 switch (ch) {
[00bb6965]295 case '\n':
[dc033a1]296 fb_pending_flush();
[0ed2e0e]297 flush_cursor = true;
[424cd43]298 cons->scr.position_y++;
299 cons->scr.position_x = 0;
[00bb6965]300 break;
301 case '\r':
302 break;
303 case '\t':
[424cd43]304 cons->scr.position_x += 8;
305 cons->scr.position_x -= cons->scr.position_x % 8;
[00bb6965]306 break;
307 case '\b':
[424cd43]308 if (cons->scr.position_x == 0)
[10569b1]309 break;
[424cd43]310 cons->scr.position_x--;
311 if (cons == active_console)
312 cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
313 screenbuffer_putchar(&cons->scr, ' ');
[00bb6965]314 break;
[1601f3c]315 default:
[424cd43]316 if (cons == active_console)
317 cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
[1601f3c]318
[424cd43]319 screenbuffer_putchar(&cons->scr, ch);
320 cons->scr.position_x++;
[10569b1]321 }
[1601f3c]322
[0ed2e0e]323 if (cons->scr.position_x >= cons->scr.size_x) {
324 flush_cursor = true;
[424cd43]325 cons->scr.position_y++;
[0ed2e0e]326 }
[10569b1]327
[424cd43]328 if (cons->scr.position_y >= cons->scr.size_y) {
[dc033a1]329 fb_pending_flush();
[424cd43]330 cons->scr.position_y = cons->scr.size_y - 1;
331 screenbuffer_clear_line(&cons->scr, cons->scr.top_line);
332 cons->scr.top_line = (cons->scr.top_line + 1) % cons->scr.size_y;
333
334 if (cons == active_console)
[79ae36dd]335 async_obsolete_msg_1(fb_info.phone, FB_SCROLL, 1);
[10569b1]336 }
[9f1362d4]337
[0ed2e0e]338 if (cons == active_console && flush_cursor)
339 curs_goto(cons->scr.position_x, cons->scr.position_y);
[424cd43]340 cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
[10569b1]341}
342
[429acb9]343/** Switch to new console */
[424cd43]344static void change_console(console_t *cons)
[429acb9]345{
[79ae36dd]346 if (cons == active_console)
[429acb9]347 return;
[1601f3c]348
[dc033a1]349 fb_pending_flush();
[1601f3c]350
[424cd43]351 if (cons == kernel_console) {
[a40dea3]352 console_serialize_start();
[8c6337d]353 curs_hide_sync();
[76fca31]354 gcons_in_kernel();
[ebfabf6]355 screen_yield();
[854eddd6]356 input_yield();
[a40dea3]357 console_serialize_end();
[1601f3c]358
[3ad953c]359 if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
360 prev_console = active_console;
[424cd43]361 active_console = kernel_console;
[3ad953c]362 } else
[424cd43]363 cons = active_console;
[01f5e17]364 }
[6d5005c]365
[424cd43]366 if (cons != kernel_console) {
[a40dea3]367 console_serialize_start();
[76fca31]368
[424cd43]369 if (active_console == kernel_console) {
[ebfabf6]370 screen_reclaim();
[854eddd6]371 input_reclaim();
[76fca31]372 gcons_redraw_console();
[10270a8]373 }
[76fca31]374
[424cd43]375 active_console = cons;
376 gcons_change_console(cons->index);
[76fca31]377
[424cd43]378 set_attrs(&cons->scr.attrs);
[8c6337d]379 curs_visibility(false);
[9f1362d4]380
[96b02eb9]381 sysarg_t x;
382 sysarg_t y;
[9f1362d4]383 int rc = 0;
384
[76fca31]385 if (interbuffer) {
[424cd43]386 for (y = 0; y < cons->scr.size_y; y++) {
387 for (x = 0; x < cons->scr.size_x; x++) {
388 interbuffer[y * cons->scr.size_x + x] =
389 *get_field_at(&cons->scr, x, y);
[76fca31]390 }
[dc033a1]391 }
[424cd43]392
[76fca31]393 /* This call can preempt, but we are already at the end */
[79ae36dd]394 rc = async_obsolete_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]395 0, 0, cons->scr.size_x,
396 cons->scr.size_y);
[76fca31]397 }
398
399 if ((!interbuffer) || (rc != 0)) {
[424cd43]400 set_attrs(&cons->scr.attrs);
401 screen_clear();
[76fca31]402
[424cd43]403 for (y = 0; y < cons->scr.size_y; y++)
404 for (x = 0; x < cons->scr.size_x; x++) {
405 keyfield_t *field = get_field_at(&cons->scr, x, y);
406
407 if (!attrs_same(cons->scr.attrs, field->attrs))
[9805cde]408 set_attrs(&field->attrs);
[424cd43]409
410 cons->scr.attrs = field->attrs;
[76fca31]411 if ((field->character == ' ') &&
[424cd43]412 (attrs_same(field->attrs, cons->scr.attrs)))
[76fca31]413 continue;
[1601f3c]414
[424cd43]415 fb_putchar(field->character, x, y);
[76fca31]416 }
417 }
418
[424cd43]419 curs_goto(cons->scr.position_x, cons->scr.position_y);
420 curs_visibility(cons->scr.is_cursor_visible);
[76fca31]421
[a40dea3]422 console_serialize_end();
[429acb9]423 }
424}
[10569b1]425
[854eddd6]426/** Handler for input events */
427static void input_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[51c1b003]428{
[424cd43]429 /* Ignore parameters, the connection is already opened */
[1601f3c]430 while (true) {
[424cd43]431 ipc_call_t call;
432 ipc_callid_t callid = async_get_call(&call);
433
434 int retval;
[79ae36dd]435 kbd_event_t ev;
[424cd43]436
[79ae36dd]437 if (!IPC_GET_IMETHOD(call)) {
[eaf34f7]438 /* TODO: Handle hangup */
[a40dea3]439 async_hangup(input_sess);
[eaf34f7]440 return;
[79ae36dd]441 }
442
443 switch (IPC_GET_IMETHOD(call)) {
[854eddd6]444 case INPUT_EVENT_KEY:
445 /* Got key press/release event */
[eaf34f7]446 retval = 0;
[fa09449]447 ev.type = IPC_GET_ARG1(call);
448 ev.key = IPC_GET_ARG2(call);
449 ev.mods = IPC_GET_ARG3(call);
450 ev.c = IPC_GET_ARG4(call);
451
[f89979b]452 if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
[0175246]453 CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
[424cd43]454 if (ev.key == KC_F1 + KERNEL_CONSOLE)
455 change_console(kernel_console);
[429acb9]456 else
[424cd43]457 change_console(&consoles[ev.key - KC_F1]);
[eaf34f7]458 break;
459 }
[cf28036c]460
[953769f]461 fibril_mutex_lock(&input_mutex);
[424cd43]462 keybuffer_push(&active_console->keybuffer, &ev);
[af65b72]463 fibril_condvar_broadcast(&input_cv);
[953769f]464 fibril_mutex_unlock(&input_mutex);
[eaf34f7]465 break;
[854eddd6]466 case INPUT_EVENT_MOVE:
467 /* Got pointer move event */
468 gcons_mouse_move((int) IPC_GET_ARG1(call),
469 (int) IPC_GET_ARG2(call));
470 retval = 0;
471 break;
472 case INPUT_EVENT_BUTTON:
473 /* Got pointer button press/release event */
[9f1362d4]474 if (IPC_GET_ARG1(call) == 1) {
475 int newcon = gcons_mouse_btn((bool) IPC_GET_ARG2(call));
[79ae36dd]476 if (newcon != -1)
[9f51afc]477 change_console(&consoles[newcon]);
478 }
479 retval = 0;
480 break;
481 default:
482 retval = ENOENT;
483 }
[ffa2c8ef]484 async_answer_0(callid, retval);
[9f51afc]485 }
486}
487
[424cd43]488static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
[d2cc7e1]489{
[472c09d]490 void *buf;
[171f9a1]491 size_t size;
[eda925a]492 int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
[1601f3c]493
[472c09d]494 if (rc != EOK) {
[ffa2c8ef]495 async_answer_0(rid, rc);
[424cd43]496 return;
497 }
[1601f3c]498
[a40dea3]499 console_serialize_start();
[1601f3c]500
[424cd43]501 size_t off = 0;
[171f9a1]502 while (off < size) {
[424cd43]503 wchar_t ch = str_decode(buf, &off, size);
504 write_char(cons, ch);
[d2cc7e1]505 }
[1601f3c]506
[a40dea3]507 console_serialize_end();
[1601f3c]508
[424cd43]509 gcons_notify_char(cons->index);
[ffa2c8ef]510 async_answer_1(rid, EOK, size);
[424cd43]511
512 free(buf);
513}
514
515static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
516{
517 ipc_callid_t callid;
518 size_t size;
[0da4e41]519 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]520 async_answer_0(callid, EINVAL);
521 async_answer_0(rid, EINVAL);
[424cd43]522 return;
523 }
524
525 char *buf = (char *) malloc(size);
526 if (buf == NULL) {
[ffa2c8ef]527 async_answer_0(callid, ENOMEM);
528 async_answer_0(rid, ENOMEM);
[424cd43]529 return;
530 }
531
532 size_t pos = 0;
[79ae36dd]533 kbd_event_t ev;
[953769f]534 fibril_mutex_lock(&input_mutex);
[9f1362d4]535
[af65b72]536recheck:
[424cd43]537 while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) {
538 if (ev.type == KEY_PRESS) {
539 buf[pos] = ev.c;
540 pos++;
541 }
542 }
543
544 if (pos == size) {
[0da4e41]545 (void) async_data_read_finalize(callid, buf, size);
[ffa2c8ef]546 async_answer_1(rid, EOK, size);
[424cd43]547 free(buf);
548 } else {
[af65b72]549 fibril_condvar_wait(&input_cv, &input_mutex);
550 goto recheck;
[424cd43]551 }
[9f1362d4]552
[953769f]553 fibril_mutex_unlock(&input_mutex);
[424cd43]554}
555
556static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
557{
[79ae36dd]558 kbd_event_t ev;
[9f1362d4]559
[953769f]560 fibril_mutex_lock(&input_mutex);
[9f1362d4]561
[af65b72]562recheck:
[424cd43]563 if (keybuffer_pop(&cons->keybuffer, &ev)) {
[ffa2c8ef]564 async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
[424cd43]565 } else {
[af65b72]566 fibril_condvar_wait(&input_cv, &input_mutex);
567 goto recheck;
[424cd43]568 }
[9f1362d4]569
[953769f]570 fibril_mutex_unlock(&input_mutex);
[d2cc7e1]571}
572
[eaf34f7]573/** Default thread for new connections */
[9934f7d]574static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[eaf34f7]575{
[424cd43]576 console_t *cons = NULL;
[3ad953c]577
[424cd43]578 size_t i;
579 for (i = 0; i < CONSOLE_COUNT; i++) {
580 if (i == KERNEL_CONSOLE)
581 continue;
582
[991f645]583 if (consoles[i].devmap_handle == (devmap_handle_t) IPC_GET_ARG1(*icall)) {
[424cd43]584 cons = &consoles[i];
585 break;
586 }
587 }
588
589 if (cons == NULL) {
[ffa2c8ef]590 async_answer_0(iid, ENOENT);
[eaf34f7]591 return;
592 }
[424cd43]593
594 ipc_callid_t callid;
595 ipc_call_t call;
[96b02eb9]596 sysarg_t arg1;
597 sysarg_t arg2;
598 sysarg_t arg3;
[9f1362d4]599
[50cfa6c]600 int rc;
[a7d2d78]601
[a40dea3]602 console_serialize_start();
[424cd43]603 if (cons->refcount == 0)
604 gcons_notify_connect(cons->index);
605
606 cons->refcount++;
[10569b1]607
[eaf34f7]608 /* Accept the connection */
[ffa2c8ef]609 async_answer_0(iid, EOK);
[3ad953c]610
[1601f3c]611 while (true) {
[a40dea3]612 console_serialize_end();
[eaf34f7]613 callid = async_get_call(&call);
[a40dea3]614 console_serialize_start();
[3ad953c]615
[96858e8]616 arg1 = 0;
617 arg2 = 0;
[fa09449]618 arg3 = 0;
[1601f3c]619
[79ae36dd]620 if (!IPC_GET_IMETHOD(call)) {
[424cd43]621 cons->refcount--;
622 if (cons->refcount == 0)
623 gcons_notify_disconnect(cons->index);
[a40dea3]624 console_serialize_end();
[eaf34f7]625 return;
[79ae36dd]626 }
627
628 switch (IPC_GET_IMETHOD(call)) {
[4198f9c3]629 case VFS_OUT_READ:
[a40dea3]630 console_serialize_end();
[424cd43]631 cons_read(cons, callid, &call);
[a40dea3]632 console_serialize_start();
[424cd43]633 continue;
[4198f9c3]634 case VFS_OUT_WRITE:
[a40dea3]635 console_serialize_end();
[424cd43]636 cons_write(cons, callid, &call);
[a40dea3]637 console_serialize_start();
[d2cc7e1]638 continue;
[4198f9c3]639 case VFS_OUT_SYNC:
[424cd43]640 fb_pending_flush();
641 if (cons == active_console) {
[79ae36dd]642 async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
[424cd43]643 curs_goto(cons->scr.position_x, cons->scr.position_y);
644 }
645 break;
[ad123964]646 case CONSOLE_CLEAR:
[3993b3d]647 /* Send message to fb */
[424cd43]648 if (cons == active_console)
[79ae36dd]649 async_obsolete_msg_0(fb_info.phone, FB_CLEAR);
[3993b3d]650
[424cd43]651 screenbuffer_clear(&cons->scr);
[3993b3d]652
[eaf34f7]653 break;
[ad123964]654 case CONSOLE_GOTO:
[424cd43]655 screenbuffer_goto(&cons->scr,
656 IPC_GET_ARG1(call), IPC_GET_ARG2(call));
657 if (cons == active_console)
[00bb6965]658 curs_goto(IPC_GET_ARG1(call),
[01f5e17]659 IPC_GET_ARG2(call));
[ad123964]660 break;
[19528516]661 case CONSOLE_GET_POS:
662 arg1 = cons->scr.position_x;
663 arg2 = cons->scr.position_y;
664 break;
[424cd43]665 case CONSOLE_GET_SIZE:
666 arg1 = fb_info.cols;
667 arg2 = fb_info.rows;
[a9bd960c]668 break;
[50cfa6c]669 case CONSOLE_GET_COLOR_CAP:
[9f1362d4]670 rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
[50cfa6c]671 if (rc != EOK) {
[ffa2c8ef]672 async_answer_0(callid, rc);
[50cfa6c]673 continue;
674 }
675 break;
[a9bd960c]676 case CONSOLE_SET_STYLE:
[dc033a1]677 fb_pending_flush();
[9805cde]678 arg1 = IPC_GET_ARG1(call);
[424cd43]679 screenbuffer_set_style(&cons->scr, arg1);
680 if (cons == active_console)
[9805cde]681 set_style(arg1);
682 break;
683 case CONSOLE_SET_COLOR:
[dc033a1]684 fb_pending_flush();
[9805cde]685 arg1 = IPC_GET_ARG1(call);
686 arg2 = IPC_GET_ARG2(call);
687 arg3 = IPC_GET_ARG3(call);
[424cd43]688 screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
689 if (cons == active_console)
[9805cde]690 set_color(arg1, arg2, arg3);
691 break;
692 case CONSOLE_SET_RGB_COLOR:
[dc033a1]693 fb_pending_flush();
[a9bd960c]694 arg1 = IPC_GET_ARG1(call);
695 arg2 = IPC_GET_ARG2(call);
[424cd43]696 screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
697 if (cons == active_console)
[9805cde]698 set_rgb_color(arg1, arg2);
[0c6984e]699 break;
[a8b2b5b2]700 case CONSOLE_CURSOR_VISIBILITY:
[dc033a1]701 fb_pending_flush();
[a8b2b5b2]702 arg1 = IPC_GET_ARG1(call);
[424cd43]703 cons->scr.is_cursor_visible = arg1;
704 if (cons == active_console)
[a8b2b5b2]705 curs_visibility(arg1);
706 break;
[424cd43]707 case CONSOLE_GET_EVENT:
[a40dea3]708 console_serialize_end();
[424cd43]709 cons_get_event(cons, callid, &call);
[a40dea3]710 console_serialize_start();
[424cd43]711 continue;
[3fe00ee]712 case CONSOLE_KCON_ENABLE:
[424cd43]713 change_console(kernel_console);
[3fe00ee]714 break;
[eaf34f7]715 }
[ffa2c8ef]716 async_answer_3(callid, EOK, arg1, arg2, arg3);
[eaf34f7]717 }
718}
719
[3ad953c]720static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
721{
722 change_console(prev_console);
723}
724
[a40dea3]725static async_sess_t *connect_input(const char *dev_path)
[eaf34f7]726{
[a40dea3]727 async_sess_t *sess;
728 async_exch_t *exch;
[79ae36dd]729 devmap_handle_t handle;
[9f1362d4]730
[854eddd6]731 int rc = devmap_device_get_handle(dev_path, &handle, 0);
[79ae36dd]732 if (rc == EOK) {
[a40dea3]733 sess = devmap_device_connect(EXCHANGE_ATOMIC, handle, 0);
734 if (sess == NULL) {
735 printf("%s: Failed to connect to input server\n", NAME);
736 return NULL;
[79ae36dd]737 }
[854eddd6]738 } else {
[a40dea3]739 return NULL;
740 }
741
742 exch = async_exchange_begin(sess);
743 if (exch == NULL) {
744 printf("%s: Failed to create callback from input server.\n", NAME);
745 return NULL;
[854eddd6]746 }
[9f1362d4]747
[79ae36dd]748 /* NB: The callback connection is slotted for removal */
[a40dea3]749 rc = async_connect_to_me(exch, SERVICE_CONSOLE, 0, 0, input_events,
750 NULL);
751
752 async_exchange_end(exch);
[854eddd6]753
[700af62]754 if (rc != EOK) {
[a40dea3]755 async_hangup(sess);
756 printf("%s: Failed to create callback from input server (%s).\n",
[79ae36dd]757 NAME, str_error(rc));
[a40dea3]758 return NULL;
[4904de8]759 }
[9f1362d4]760
[a40dea3]761 return sess;
[700af62]762}
763
[854eddd6]764static bool console_srv_init(char *input_dev)
[700af62]765{
[854eddd6]766 /* Connect to input server */
[a40dea3]767 input_sess = connect_input(input_dev);
768 if (input_sess == NULL)
[700af62]769 return false;
[79ae36dd]770
[51c1b003]771 /* Connect to framebuffer driver */
[79ae36dd]772 fb_info.phone = service_obsolete_connect_blocking(SERVICE_VIDEO, 0, 0);
[4904de8]773 if (fb_info.phone < 0) {
[79ae36dd]774 printf("%s: Failed to connect to video service\n", NAME);
775 return false;
[3993b3d]776 }
[9f1362d4]777
[424cd43]778 /* Register driver */
779 int rc = devmap_driver_register(NAME, client_connection);
780 if (rc < 0) {
[79ae36dd]781 printf("%s: Unable to register driver (%d)\n", NAME, rc);
[424cd43]782 return false;
783 }
[516ff92]784
[e1c4849]785 /* Initialize gcons */
786 gcons_init(fb_info.phone);
[3993b3d]787
[424cd43]788 /* Synchronize, the gcons could put something in queue */
[79ae36dd]789 async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
790 async_obsolete_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
791 async_obsolete_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
[1601f3c]792
[7447572]793 /* Set up shared memory buffer. */
[424cd43]794 size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
[7447572]795 interbuffer = as_get_mappable_page(ib_size);
[1601f3c]796
[7447572]797 if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
[424cd43]798 AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
[7447572]799 interbuffer = NULL;
[1601f3c]800
[7447572]801 if (interbuffer) {
[79ae36dd]802 if (async_obsolete_share_out_start(fb_info.phone, interbuffer,
[27d293a]803 AS_AREA_READ) != EOK) {
[7447572]804 as_area_destroy(interbuffer);
[390a678]805 interbuffer = NULL;
806 }
807 }
[3ad953c]808
[424cd43]809 fb_pending.cnt = 0;
[3ad953c]810
[424cd43]811 /* Inititalize consoles */
812 size_t i;
813 for (i = 0; i < CONSOLE_COUNT; i++) {
814 if (i != KERNEL_CONSOLE) {
815 if (screenbuffer_init(&consoles[i].scr,
816 fb_info.cols, fb_info.rows) == NULL) {
[79ae36dd]817 printf("%s: Unable to allocate screen buffer %zu\n", NAME, i);
[424cd43]818 return false;
819 }
820 screenbuffer_clear(&consoles[i].scr);
821 keybuffer_init(&consoles[i].keybuffer);
822 consoles[i].index = i;
823 consoles[i].refcount = 0;
824
[1313ee9]825 char vc[DEVMAP_NAME_MAXLEN + 1];
[7e752b2]826 snprintf(vc, DEVMAP_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
[424cd43]827
[991f645]828 if (devmap_device_register(vc, &consoles[i].devmap_handle) != EOK) {
[79ae36dd]829 printf("%s: Unable to register device %s\n", NAME, vc);
[424cd43]830 return false;
831 }
832 }
833 }
834
[1035437]835 /* Disable kernel output to the console */
836 __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
837
[424cd43]838 /* Initialize the screen */
[a40dea3]839 console_serialize_start();
[1035437]840 gcons_redraw_console();
[369a5f8]841 set_style(STYLE_NORMAL);
[424cd43]842 screen_clear();
843 curs_goto(0, 0);
844 curs_visibility(active_console->scr.is_cursor_visible);
[a40dea3]845 console_serialize_end();
[51c1b003]846
[3ad953c]847 /* Receive kernel notifications */
[007e6efa]848 async_set_interrupt_received(interrupt_received);
[05641a9e]849 if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
[79ae36dd]850 printf("%s: Error registering kconsole notifications\n", NAME);
[1601f3c]851
[424cd43]852 return true;
853}
854
[47a350f]855static void usage(void)
856{
[854eddd6]857 printf("Usage: console <input_dev>\n");
[47a350f]858}
859
[424cd43]860int main(int argc, char *argv[])
861{
[47a350f]862 if (argc < 2) {
863 usage();
864 return -1;
865 }
866
[424cd43]867 printf(NAME ": HelenOS Console service\n");
868
[79ae36dd]869 if (!console_srv_init(argv[1]))
[424cd43]870 return -1;
[79ae36dd]871
[424cd43]872 printf(NAME ": Accepting connections\n");
[eaf34f7]873 async_manager();
[3ad953c]874
875 return 0;
[51c1b003]876}
[516ff92]877
[ce5bcb4]878/** @}
879 */
Note: See TracBrowser for help on using the repository browser.