source: mainline/uspace/srv/hid/console/console.c@ 96b02eb9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96b02eb9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

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