source: mainline/uspace/srv/hid/console/console.c@ 86ffa27f

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

Merge mainline changes.

  • Property mode set to 100644
File size: 20.6 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>
[15f3c3f]55#include <loc.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 */
[15f3c3f]83 service_id_t service_id; /**< Service ID */
[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
[b366a6f4]359 if (console_kcon()) {
[3ad953c]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 }
[1875a0c]484
[ffa2c8ef]485 async_answer_0(callid, retval);
[9f51afc]486 }
487}
488
[424cd43]489static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
[d2cc7e1]490{
[472c09d]491 void *buf;
[171f9a1]492 size_t size;
[eda925a]493 int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
[1601f3c]494
[472c09d]495 if (rc != EOK) {
[ffa2c8ef]496 async_answer_0(rid, rc);
[424cd43]497 return;
498 }
[1601f3c]499
[a40dea3]500 console_serialize_start();
[1601f3c]501
[424cd43]502 size_t off = 0;
[171f9a1]503 while (off < size) {
[424cd43]504 wchar_t ch = str_decode(buf, &off, size);
505 write_char(cons, ch);
[d2cc7e1]506 }
[1601f3c]507
[a40dea3]508 console_serialize_end();
[1601f3c]509
[424cd43]510 gcons_notify_char(cons->index);
[ffa2c8ef]511 async_answer_1(rid, EOK, size);
[424cd43]512
513 free(buf);
514}
515
516static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
517{
518 ipc_callid_t callid;
519 size_t size;
[0da4e41]520 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]521 async_answer_0(callid, EINVAL);
522 async_answer_0(rid, EINVAL);
[424cd43]523 return;
524 }
525
526 char *buf = (char *) malloc(size);
527 if (buf == NULL) {
[ffa2c8ef]528 async_answer_0(callid, ENOMEM);
529 async_answer_0(rid, ENOMEM);
[424cd43]530 return;
531 }
532
533 size_t pos = 0;
[79ae36dd]534 kbd_event_t ev;
[953769f]535 fibril_mutex_lock(&input_mutex);
[9f1362d4]536
[af65b72]537recheck:
[424cd43]538 while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) {
539 if (ev.type == KEY_PRESS) {
540 buf[pos] = ev.c;
541 pos++;
542 }
543 }
544
545 if (pos == size) {
[0da4e41]546 (void) async_data_read_finalize(callid, buf, size);
[ffa2c8ef]547 async_answer_1(rid, EOK, size);
[424cd43]548 free(buf);
549 } else {
[af65b72]550 fibril_condvar_wait(&input_cv, &input_mutex);
551 goto recheck;
[424cd43]552 }
[9f1362d4]553
[953769f]554 fibril_mutex_unlock(&input_mutex);
[424cd43]555}
556
557static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
558{
[79ae36dd]559 kbd_event_t ev;
[9f1362d4]560
[953769f]561 fibril_mutex_lock(&input_mutex);
[9f1362d4]562
[af65b72]563recheck:
[424cd43]564 if (keybuffer_pop(&cons->keybuffer, &ev)) {
[ffa2c8ef]565 async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
[424cd43]566 } else {
[af65b72]567 fibril_condvar_wait(&input_cv, &input_mutex);
568 goto recheck;
[424cd43]569 }
[9f1362d4]570
[953769f]571 fibril_mutex_unlock(&input_mutex);
[d2cc7e1]572}
573
[eaf34f7]574/** Default thread for new connections */
[9934f7d]575static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[eaf34f7]576{
[424cd43]577 console_t *cons = NULL;
[3ad953c]578
[424cd43]579 size_t i;
580 for (i = 0; i < CONSOLE_COUNT; i++) {
581 if (i == KERNEL_CONSOLE)
582 continue;
583
[15f3c3f]584 if (consoles[i].service_id == (service_id_t) IPC_GET_ARG1(*icall)) {
[424cd43]585 cons = &consoles[i];
586 break;
587 }
588 }
589
590 if (cons == NULL) {
[ffa2c8ef]591 async_answer_0(iid, ENOENT);
[eaf34f7]592 return;
593 }
[424cd43]594
595 ipc_callid_t callid;
596 ipc_call_t call;
[96b02eb9]597 sysarg_t arg1;
598 sysarg_t arg2;
599 sysarg_t arg3;
[9f1362d4]600
[50cfa6c]601 int rc;
[a7d2d78]602
[a40dea3]603 console_serialize_start();
[424cd43]604 if (cons->refcount == 0)
605 gcons_notify_connect(cons->index);
606
607 cons->refcount++;
[10569b1]608
[eaf34f7]609 /* Accept the connection */
[ffa2c8ef]610 async_answer_0(iid, EOK);
[3ad953c]611
[1601f3c]612 while (true) {
[a40dea3]613 console_serialize_end();
[eaf34f7]614 callid = async_get_call(&call);
[a40dea3]615 console_serialize_start();
[3ad953c]616
[96858e8]617 arg1 = 0;
618 arg2 = 0;
[fa09449]619 arg3 = 0;
[1601f3c]620
[79ae36dd]621 if (!IPC_GET_IMETHOD(call)) {
[424cd43]622 cons->refcount--;
623 if (cons->refcount == 0)
624 gcons_notify_disconnect(cons->index);
[a40dea3]625 console_serialize_end();
[eaf34f7]626 return;
[79ae36dd]627 }
628
629 switch (IPC_GET_IMETHOD(call)) {
[4198f9c3]630 case VFS_OUT_READ:
[a40dea3]631 console_serialize_end();
[424cd43]632 cons_read(cons, callid, &call);
[a40dea3]633 console_serialize_start();
[424cd43]634 continue;
[4198f9c3]635 case VFS_OUT_WRITE:
[a40dea3]636 console_serialize_end();
[424cd43]637 cons_write(cons, callid, &call);
[a40dea3]638 console_serialize_start();
[d2cc7e1]639 continue;
[4198f9c3]640 case VFS_OUT_SYNC:
[424cd43]641 fb_pending_flush();
642 if (cons == active_console) {
[79ae36dd]643 async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
[424cd43]644 curs_goto(cons->scr.position_x, cons->scr.position_y);
645 }
646 break;
[ad123964]647 case CONSOLE_CLEAR:
[3993b3d]648 /* Send message to fb */
[424cd43]649 if (cons == active_console)
[79ae36dd]650 async_obsolete_msg_0(fb_info.phone, FB_CLEAR);
[3993b3d]651
[424cd43]652 screenbuffer_clear(&cons->scr);
[3993b3d]653
[eaf34f7]654 break;
[ad123964]655 case CONSOLE_GOTO:
[424cd43]656 screenbuffer_goto(&cons->scr,
657 IPC_GET_ARG1(call), IPC_GET_ARG2(call));
658 if (cons == active_console)
[00bb6965]659 curs_goto(IPC_GET_ARG1(call),
[01f5e17]660 IPC_GET_ARG2(call));
[ad123964]661 break;
[19528516]662 case CONSOLE_GET_POS:
663 arg1 = cons->scr.position_x;
664 arg2 = cons->scr.position_y;
665 break;
[424cd43]666 case CONSOLE_GET_SIZE:
667 arg1 = fb_info.cols;
668 arg2 = fb_info.rows;
[a9bd960c]669 break;
[50cfa6c]670 case CONSOLE_GET_COLOR_CAP:
[9f1362d4]671 rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
[50cfa6c]672 if (rc != EOK) {
[ffa2c8ef]673 async_answer_0(callid, rc);
[50cfa6c]674 continue;
675 }
676 break;
[a9bd960c]677 case CONSOLE_SET_STYLE:
[dc033a1]678 fb_pending_flush();
[9805cde]679 arg1 = IPC_GET_ARG1(call);
[424cd43]680 screenbuffer_set_style(&cons->scr, arg1);
681 if (cons == active_console)
[9805cde]682 set_style(arg1);
683 break;
684 case CONSOLE_SET_COLOR:
[dc033a1]685 fb_pending_flush();
[9805cde]686 arg1 = IPC_GET_ARG1(call);
687 arg2 = IPC_GET_ARG2(call);
688 arg3 = IPC_GET_ARG3(call);
[424cd43]689 screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
690 if (cons == active_console)
[9805cde]691 set_color(arg1, arg2, arg3);
692 break;
693 case CONSOLE_SET_RGB_COLOR:
[dc033a1]694 fb_pending_flush();
[a9bd960c]695 arg1 = IPC_GET_ARG1(call);
696 arg2 = IPC_GET_ARG2(call);
[424cd43]697 screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
698 if (cons == active_console)
[9805cde]699 set_rgb_color(arg1, arg2);
[0c6984e]700 break;
[a8b2b5b2]701 case CONSOLE_CURSOR_VISIBILITY:
[dc033a1]702 fb_pending_flush();
[a8b2b5b2]703 arg1 = IPC_GET_ARG1(call);
[424cd43]704 cons->scr.is_cursor_visible = arg1;
705 if (cons == active_console)
[a8b2b5b2]706 curs_visibility(arg1);
707 break;
[424cd43]708 case CONSOLE_GET_EVENT:
[a40dea3]709 console_serialize_end();
[424cd43]710 cons_get_event(cons, callid, &call);
[a40dea3]711 console_serialize_start();
[424cd43]712 continue;
[eaf34f7]713 }
[ffa2c8ef]714 async_answer_3(callid, EOK, arg1, arg2, arg3);
[eaf34f7]715 }
716}
717
[3ad953c]718static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
719{
720 change_console(prev_console);
721}
722
[15f3c3f]723static async_sess_t *connect_input(const char *svc_path)
[eaf34f7]724{
[a40dea3]725 async_sess_t *sess;
726 async_exch_t *exch;
[15f3c3f]727 service_id_t service_id;
[9f1362d4]728
[15f3c3f]729 int rc = loc_service_get_id(svc_path, &service_id, 0);
[79ae36dd]730 if (rc == EOK) {
[15f3c3f]731 sess = loc_service_connect(EXCHANGE_ATOMIC, service_id, 0);
[a40dea3]732 if (sess == NULL) {
733 printf("%s: Failed to connect to input server\n", NAME);
734 return NULL;
[79ae36dd]735 }
[854eddd6]736 } else {
[a40dea3]737 return NULL;
738 }
739
740 exch = async_exchange_begin(sess);
741 if (exch == NULL) {
742 printf("%s: Failed to create callback from input server.\n", NAME);
743 return NULL;
[854eddd6]744 }
[9f1362d4]745
[79ae36dd]746 /* NB: The callback connection is slotted for removal */
[3015f4d]747 rc = async_connect_to_me(exch, 0, 0, 0, input_events, NULL);
[a40dea3]748
749 async_exchange_end(exch);
[854eddd6]750
[700af62]751 if (rc != EOK) {
[a40dea3]752 async_hangup(sess);
753 printf("%s: Failed to create callback from input server (%s).\n",
[79ae36dd]754 NAME, str_error(rc));
[a40dea3]755 return NULL;
[4904de8]756 }
[9f1362d4]757
[a40dea3]758 return sess;
[700af62]759}
760
[854eddd6]761static bool console_srv_init(char *input_dev)
[700af62]762{
[854eddd6]763 /* Connect to input server */
[a40dea3]764 input_sess = connect_input(input_dev);
765 if (input_sess == NULL)
[700af62]766 return false;
[79ae36dd]767
[51c1b003]768 /* Connect to framebuffer driver */
[79ae36dd]769 fb_info.phone = service_obsolete_connect_blocking(SERVICE_VIDEO, 0, 0);
[4904de8]770 if (fb_info.phone < 0) {
[79ae36dd]771 printf("%s: Failed to connect to video service\n", NAME);
772 return false;
[3993b3d]773 }
[9f1362d4]774
[15f3c3f]775 /* Register server */
776 int rc = loc_server_register(NAME, client_connection);
[424cd43]777 if (rc < 0) {
[15f3c3f]778 printf("%s: Unable to register server (%d)\n", NAME, rc);
[424cd43]779 return false;
780 }
[516ff92]781
[e1c4849]782 /* Initialize gcons */
783 gcons_init(fb_info.phone);
[3993b3d]784
[424cd43]785 /* Synchronize, the gcons could put something in queue */
[79ae36dd]786 async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
787 async_obsolete_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
788 async_obsolete_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
[1601f3c]789
[7447572]790 /* Set up shared memory buffer. */
[424cd43]791 size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
[7447572]792 interbuffer = as_get_mappable_page(ib_size);
[1601f3c]793
[7447572]794 if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
[424cd43]795 AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
[7447572]796 interbuffer = NULL;
[1601f3c]797
[7447572]798 if (interbuffer) {
[79ae36dd]799 if (async_obsolete_share_out_start(fb_info.phone, interbuffer,
[27d293a]800 AS_AREA_READ) != EOK) {
[7447572]801 as_area_destroy(interbuffer);
[390a678]802 interbuffer = NULL;
803 }
804 }
[3ad953c]805
[424cd43]806 fb_pending.cnt = 0;
[3ad953c]807
[424cd43]808 /* Inititalize consoles */
809 size_t i;
810 for (i = 0; i < CONSOLE_COUNT; i++) {
811 if (i != KERNEL_CONSOLE) {
812 if (screenbuffer_init(&consoles[i].scr,
813 fb_info.cols, fb_info.rows) == NULL) {
[79ae36dd]814 printf("%s: Unable to allocate screen buffer %zu\n", NAME, i);
[424cd43]815 return false;
816 }
817 screenbuffer_clear(&consoles[i].scr);
818 keybuffer_init(&consoles[i].keybuffer);
819 consoles[i].index = i;
820 consoles[i].refcount = 0;
821
[15f3c3f]822 char vc[LOC_NAME_MAXLEN + 1];
823 snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
[424cd43]824
[15f3c3f]825 if (loc_service_register(vc, &consoles[i].service_id) != EOK) {
826 printf("%s: Unable to register service %s\n", NAME, vc);
[424cd43]827 return false;
828 }
829 }
830 }
831
832 /* Initialize the screen */
[a40dea3]833 console_serialize_start();
[1035437]834 gcons_redraw_console();
[369a5f8]835 set_style(STYLE_NORMAL);
[424cd43]836 screen_clear();
837 curs_goto(0, 0);
838 curs_visibility(active_console->scr.is_cursor_visible);
[a40dea3]839 console_serialize_end();
[51c1b003]840
[3ad953c]841 /* Receive kernel notifications */
[007e6efa]842 async_set_interrupt_received(interrupt_received);
[05641a9e]843 if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
[79ae36dd]844 printf("%s: Error registering kconsole notifications\n", NAME);
[1601f3c]845
[424cd43]846 return true;
847}
848
[47a350f]849static void usage(void)
850{
[854eddd6]851 printf("Usage: console <input_dev>\n");
[47a350f]852}
853
[424cd43]854int main(int argc, char *argv[])
855{
[47a350f]856 if (argc < 2) {
857 usage();
858 return -1;
859 }
860
[424cd43]861 printf(NAME ": HelenOS Console service\n");
862
[79ae36dd]863 if (!console_srv_init(argv[1]))
[424cd43]864 return -1;
[79ae36dd]865
[424cd43]866 printf(NAME ": Accepting connections\n");
[eaf34f7]867 async_manager();
[3ad953c]868
869 return 0;
[51c1b003]870}
[516ff92]871
[ce5bcb4]872/** @}
873 */
Note: See TracBrowser for help on using the repository browser.