source: mainline/uspace/srv/console/console.c@ d797054c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d797054c was 0175246, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

Primitive means of switching keyboard layout at run time. Use Ctrl+Fn, 1 = QWERTY, 2 = Dvorak, 3 = Czech. Remove compile-time option.

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