source: mainline/uspace/srv/console/console.c@ 3ad953c

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

send notification to uspace console when switching from kernel console

  • Property mode set to 100644
File size: 13.6 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>
[15039b67]38#include <keys.h>
[79460ae]39#include <ipc/fb.h>
[51c1b003]40#include <ipc/services.h>
41#include <errno.h>
[79460ae]42#include <key_buffer.h>
43#include <console.h>
[d3e6935]44#include <ipc/console.h>
[eaf34f7]45#include <unistd.h>
46#include <async.h>
[cf28036c]47#include <libadt/fifo.h>
[3993b3d]48#include <screenbuffer.h>
[2def788]49#include <sys/mman.h>
[271b540]50#include <stdio.h>
[3ad953c]51#include <sysinfo.h>
[79460ae]52
[e1c4849]53#include "gcons.h"
54
[cf28036c]55#define MAX_KEYREQUESTS_BUFFERED 32
[51c1b003]56
[271b540]57#define NAME "console"
[51c1b003]58
[e87e18f]59/** Index of currently used virtual console.
60 */
[390a678]61int active_console = 0;
[3ad953c]62int prev_console = 0;
[eaf34f7]63
[e87e18f]64/** Information about framebuffer
65 */
[3993b3d]66struct {
67 int phone; /**< Framebuffer phone */
[bb51e9a8]68 ipcarg_t rows; /**< Framebuffer rows */
69 ipcarg_t cols; /**< Framebuffer columns */
[3993b3d]70} fb_info;
71
[79460ae]72typedef struct {
[e87e18f]73 keybuffer_t keybuffer; /**< Buffer for incoming keys. */
[00bb6965]74 /** Buffer for unsatisfied request for keys. */
75 FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
76 MAX_KEYREQUESTS_BUFFERED);
[e87e18f]77 int keyrequest_counter; /**< Number of requests in buffer. */
78 int client_phone; /**< Phone to connected client. */
[00bb6965]79 int used; /**< 1 if this virtual console is
80 * connected to some client.*/
81 screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen
82 * contents and related settings. */
[79460ae]83} connection_t;
84
[00bb6965]85static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
86 * consoles */
87static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared
88 * with framebufer used for
89 * faster virtual console
[c738d65]90 * switching */
[429acb9]91
[bb51e9a8]92
[e87e18f]93/** Find unused virtual console.
94 *
95 */
[d32af35]96static int find_free_connection(void)
[79460ae]97{
[c738d65]98 int i;
[79460ae]99
[c738d65]100 for (i = 0; i < CONSOLE_COUNT; i++) {
[d32af35]101 if (!connections[i].used)
[79460ae]102 return i;
103 }
[d32af35]104 return -1;
[79460ae]105}
106
[429acb9]107static void clrscr(void)
108{
[0cc4313]109 async_msg_0(fb_info.phone, FB_CLEAR);
[429acb9]110}
111
[8c6337d]112static void curs_visibility(bool visible)
[429acb9]113{
[8c6337d]114 async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
115}
116
117static void curs_hide_sync(void)
118{
119 ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
[429acb9]120}
121
122static void curs_goto(int row, int col)
123{
[085bd54]124 async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
[429acb9]125}
126
127static void set_style(style_t *style)
128{
[00bb6965]129 async_msg_2(fb_info.phone, FB_SET_STYLE, style->fg_color,
[01f5e17]130 style->bg_color);
[429acb9]131}
132
133static void set_style_col(int fgcolor, int bgcolor)
134{
[085bd54]135 async_msg_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
[429acb9]136}
137
138static void prtchr(char c, int row, int col)
139{
[085bd54]140 async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
[429acb9]141}
142
[10569b1]143/** Check key and process special keys.
144 *
[96858e8]145 *
146 */
[10569b1]147static void write_char(int console, char key)
148{
149 screenbuffer_t *scr = &(connections[console].screenbuffer);
150
151 switch (key) {
[00bb6965]152 case '\n':
[c738d65]153 scr->position_y++;
154 scr->position_x = 0;
[00bb6965]155 break;
156 case '\r':
157 break;
158 case '\t':
159 scr->position_x += 8;
160 scr->position_x -= scr->position_x % 8;
161 break;
162 case '\b':
163 if (scr->position_x == 0)
[10569b1]164 break;
[00bb6965]165 scr->position_x--;
166 if (console == active_console)
167 prtchr(' ', scr->position_y, scr->position_x);
168 screenbuffer_putchar(scr, ' ');
169 break;
170 default:
171 if (console == active_console)
172 prtchr(key, scr->position_y, scr->position_x);
[10569b1]173
[00bb6965]174 screenbuffer_putchar(scr, key);
175 scr->position_x++;
[10569b1]176 }
177
178 scr->position_y += (scr->position_x >= scr->size_x);
179
180 if (scr->position_y >= scr->size_y) {
181 scr->position_y = scr->size_y - 1;
[c891ed39]182 screenbuffer_clear_line(scr, scr->top_line);
[c738d65]183 scr->top_line = (scr->top_line + 1) % scr->size_y;
[b1f51f0]184 if (console == active_console)
[0cc4313]185 async_msg_1(fb_info.phone, FB_SCROLL, 1);
[10569b1]186 }
187
188 scr->position_x = scr->position_x % scr->size_x;
[bd929cfb]189
[429acb9]190 if (console == active_console)
191 curs_goto(scr->position_y, scr->position_x);
[10569b1]192
193}
194
[429acb9]195/** Switch to new console */
196static void change_console(int newcons)
197{
198 connection_t *conn;
[bd02038]199 int i, j, rc;
[6d5005c]200 keyfield_t *field;
201 style_t *style;
[76fca31]202
[429acb9]203 if (newcons == active_console)
204 return;
[76fca31]205
[a7d2d78]206 if (newcons == KERNEL_CONSOLE) {
[1f83244]207 async_serialize_start();
[8c6337d]208 curs_hide_sync();
[76fca31]209 gcons_in_kernel();
[1f83244]210 async_serialize_end();
[76fca31]211
[3ad953c]212 if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
213 prev_console = active_console;
[76fca31]214 active_console = KERNEL_CONSOLE;
[3ad953c]215 } else
[8c6337d]216 newcons = active_console;
[01f5e17]217 }
[6d5005c]218
[76fca31]219 if (newcons != KERNEL_CONSOLE) {
220 async_serialize_start();
221
222 if (active_console == KERNEL_CONSOLE)
223 gcons_redraw_console();
224
225 active_console = newcons;
226 gcons_change_console(newcons);
227 conn = &connections[active_console];
228
[6d5005c]229 set_style(&conn->screenbuffer.style);
[8c6337d]230 curs_visibility(false);
[76fca31]231 if (interbuffer) {
232 for (i = 0; i < conn->screenbuffer.size_x; i++)
233 for (j = 0; j < conn->screenbuffer.size_y; j++) {
234 unsigned int size_x;
235
236 size_x = conn->screenbuffer.size_x;
237 interbuffer[i + j * size_x] =
238 *get_field_at(&conn->screenbuffer, i, j);
239 }
240 /* This call can preempt, but we are already at the end */
[3ad953c]241 rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA);
[76fca31]242 }
243
244 if ((!interbuffer) || (rc != 0)) {
245 set_style(&conn->screenbuffer.style);
246 clrscr();
247 style = &conn->screenbuffer.style;
248
[3ad953c]249 for (j = 0; j < conn->screenbuffer.size_y; j++)
[76fca31]250 for (i = 0; i < conn->screenbuffer.size_x; i++) {
251 field = get_field_at(&conn->screenbuffer, i, j);
252 if (!style_same(*style, field->style))
253 set_style(&field->style);
254 style = &field->style;
255 if ((field->character == ' ') &&
256 (style_same(field->style,
257 conn->screenbuffer.style)))
258 continue;
259
260 prtchr(field->character, j, i);
261 }
262 }
263
264 curs_goto(conn->screenbuffer.position_y,
265 conn->screenbuffer.position_x);
266 curs_visibility(conn->screenbuffer.is_cursor_visible);
267
268 async_serialize_end();
[429acb9]269 }
270}
[10569b1]271
[e87e18f]272/** Handler for keyboard */
[eaf34f7]273static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
[51c1b003]274{
[eaf34f7]275 ipc_callid_t callid;
[51c1b003]276 ipc_call_t call;
[eaf34f7]277 int retval;
[dd641e3]278 int c;
[c1d2c9d]279 connection_t *conn;
[1f83244]280 int newcon;
[bb51e9a8]281
[eaf34f7]282 /* Ignore parameters, the connection is alread opened */
283 while (1) {
284 callid = async_get_call(&call);
285 switch (IPC_GET_METHOD(call)) {
286 case IPC_M_PHONE_HUNGUP:
287 /* TODO: Handle hangup */
288 return;
[1f83244]289 case KBD_MS_LEFT:
290 newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
291 if (newcon != -1)
292 change_console(newcon);
[501a8ba]293 retval = 0;
[1f83244]294 break;
[830ac99]295 case KBD_MS_MOVE:
[00bb6965]296 gcons_mouse_move(IPC_GET_ARG1(call),
[01f5e17]297 IPC_GET_ARG2(call));
[501a8ba]298 retval = 0;
[830ac99]299 break;
[eaf34f7]300 case KBD_PUSHCHAR:
301 /* got key from keyboard driver */
302 retval = 0;
[a805c24]303 c = IPC_GET_ARG1(call);
[eaf34f7]304 /* switch to another virtual console */
[cf28036c]305
[c1d2c9d]306 conn = &connections[active_console];
[00bb6965]307/*
308 * if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
309 * CONSOLE_COUNT)) {
310 */
[dd641e3]311 if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
312 if (c == 0x112)
[a7d2d78]313 change_console(KERNEL_CONSOLE);
[429acb9]314 else
[dd641e3]315 change_console(c - 0x101);
[eaf34f7]316 break;
317 }
[cf28036c]318
319 /* if client is awaiting key, send it */
[c1d2c9d]320 if (conn->keyrequest_counter > 0) {
321 conn->keyrequest_counter--;
[b74959bd]322 ipc_answer_1(fifo_pop(conn->keyrequests), EOK,
323 c);
[cf28036c]324 break;
325 }
326
[c1d2c9d]327 keybuffer_push(&conn->keybuffer, c);
[501a8ba]328 retval = 0;
[ad123964]329
[eaf34f7]330 break;
331 default:
[1029d3d3]332 retval = ENOENT;
[085bd54]333 }
[b74959bd]334 ipc_answer_0(callid, retval);
[eaf34f7]335 }
336}
337
338/** Default thread for new connections */
[b1f51f0]339static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
[eaf34f7]340{
[51c1b003]341 ipc_callid_t callid;
[eaf34f7]342 ipc_call_t call;
343 int consnum;
[3756912]344 ipcarg_t arg1, arg2;
[e9073f2]345 connection_t *conn;
[3ad953c]346
[d32af35]347 if ((consnum = find_free_connection()) == -1) {
[b74959bd]348 ipc_answer_0(iid, ELIMIT);
[eaf34f7]349 return;
350 }
[e9073f2]351 conn = &connections[consnum];
[085bd54]352 conn->used = 1;
[a7d2d78]353
[085bd54]354 async_serialize_start();
[a7d2d78]355 gcons_notify_connect(consnum);
[38c706cc]356 conn->client_phone = IPC_GET_ARG5(*icall);
[e9073f2]357 screenbuffer_clear(&conn->screenbuffer);
[10569b1]358
[eaf34f7]359 /* Accept the connection */
[b74959bd]360 ipc_answer_0(iid, EOK);
[3ad953c]361
[eaf34f7]362 while (1) {
[085bd54]363 async_serialize_end();
[eaf34f7]364 callid = async_get_call(&call);
[085bd54]365 async_serialize_start();
[3ad953c]366
[96858e8]367 arg1 = 0;
368 arg2 = 0;
[eaf34f7]369 switch (IPC_GET_METHOD(call)) {
370 case IPC_M_PHONE_HUNGUP:
[e9073f2]371 gcons_notify_disconnect(consnum);
[085bd54]372
[e9073f2]373 /* Answer all pending requests */
[3ad953c]374 while (conn->keyrequest_counter > 0) {
[e9073f2]375 conn->keyrequest_counter--;
[b74959bd]376 ipc_answer_0(fifo_pop(conn->keyrequests),
377 ENOENT);
[e9073f2]378 break;
379 }
[0d11fc1c]380 conn->used = 0;
[eaf34f7]381 return;
382 case CONSOLE_PUTCHAR:
[10569b1]383 write_char(consnum, IPC_GET_ARG1(call));
[d6cc453]384 gcons_notify_char(consnum);
[ad123964]385 break;
386 case CONSOLE_CLEAR:
[3993b3d]387 /* Send message to fb */
388 if (consnum == active_console) {
[0cc4313]389 async_msg_0(fb_info.phone, FB_CLEAR);
[3993b3d]390 }
391
[e9073f2]392 screenbuffer_clear(&conn->screenbuffer);
[3993b3d]393
[eaf34f7]394 break;
[ad123964]395 case CONSOLE_GOTO:
[00bb6965]396 screenbuffer_goto(&conn->screenbuffer,
[01f5e17]397 IPC_GET_ARG2(call), IPC_GET_ARG1(call));
[6118e5f6]398 if (consnum == active_console)
[00bb6965]399 curs_goto(IPC_GET_ARG1(call),
[01f5e17]400 IPC_GET_ARG2(call));
[ad123964]401 break;
[3756912]402 case CONSOLE_GETSIZE:
[d6cc453]403 arg1 = fb_info.rows;
404 arg2 = fb_info.cols;
[3756912]405 break;
[0c6984e]406 case CONSOLE_FLUSH:
[a3d2939]407 if (consnum == active_console)
[0cc4313]408 async_req_0_0(fb_info.phone, FB_FLUSH);
[a9bd960c]409 break;
410 case CONSOLE_SET_STYLE:
411 arg1 = IPC_GET_ARG1(call);
412 arg2 = IPC_GET_ARG2(call);
[01f5e17]413 screenbuffer_set_style(&conn->screenbuffer, arg1,
414 arg2);
[a9bd960c]415 if (consnum == active_console)
[429acb9]416 set_style_col(arg1, arg2);
[0c6984e]417 break;
[a8b2b5b2]418 case CONSOLE_CURSOR_VISIBILITY:
419 arg1 = IPC_GET_ARG1(call);
[e9073f2]420 conn->screenbuffer.is_cursor_visible = arg1;
[a8b2b5b2]421 if (consnum == active_console)
422 curs_visibility(arg1);
423 break;
[eaf34f7]424 case CONSOLE_GETCHAR:
[e9073f2]425 if (keybuffer_empty(&conn->keybuffer)) {
[cf28036c]426 /* buffer is empty -> store request */
[00bb6965]427 if (conn->keyrequest_counter <
428 MAX_KEYREQUESTS_BUFFERED) {
[e9073f2]429 fifo_push(conn->keyrequests, callid);
430 conn->keyrequest_counter++;
[cf28036c]431 } else {
[00bb6965]432 /*
433 * No key available and too many
434 * requests => fail.
435 */
[b74959bd]436 ipc_answer_0(callid, ELIMIT);
[cf28036c]437 }
438 continue;
[00bb6965]439 }
[18c485a]440 int ch;
441 keybuffer_pop(&conn->keybuffer, &ch);
442 arg1 = ch;
[eaf34f7]443 break;
444 }
[b74959bd]445 ipc_answer_2(callid, EOK, arg1, arg2);
[eaf34f7]446 }
447}
448
[3ad953c]449static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
450{
451 change_console(prev_console);
452}
453
[eaf34f7]454int main(int argc, char *argv[])
455{
[271b540]456 printf(NAME ": HelenOS Console service\n");
457
[eaf34f7]458 ipcarg_t phonehash;
[501a8ba]459 int kbd_phone;
[79460ae]460 int i;
[3ad953c]461
[da0c91e7]462 async_set_client_connection(client_connection);
[51c1b003]463
464 /* Connect to keyboard driver */
[3ad953c]465
[b61d47d]466 kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
[c738d65]467 while (kbd_phone < 0) {
[eaf34f7]468 usleep(10000);
[b61d47d]469 kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
[00bb6965]470 }
[51c1b003]471
[38c706cc]472 if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
[51c1b003]473 return -1;
[290c0db]474 async_new_connection(phonehash, 0, NULL, keyboard_events);
475
[51c1b003]476 /* Connect to framebuffer driver */
[b61d47d]477 fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
[c738d65]478 while (fb_info.phone < 0) {
[3993b3d]479 usleep(10000);
[b61d47d]480 fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
[3993b3d]481 }
[429acb9]482
[e1c4849]483 /* Initialize gcons */
484 gcons_init(fb_info.phone);
485 /* Synchronize, the gcons can have something in queue */
[0cc4313]486 async_req_0_0(fb_info.phone, FB_FLUSH);
[3993b3d]487
[0cc4313]488 async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
[01f5e17]489 &fb_info.cols);
[429acb9]490 set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
491 clrscr();
[3993b3d]492
493 /* Init virtual consoles */
[79460ae]494 for (i = 0; i < CONSOLE_COUNT; i++) {
495 connections[i].used = 0;
[01f5e17]496 keybuffer_init(&connections[i].keybuffer);
[cf28036c]497
[01f5e17]498 connections[i].keyrequests.head = 0;
499 connections[i].keyrequests.tail = 0;
[cf28036c]500 connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
501 connections[i].keyrequest_counter = 0;
[3993b3d]502
[01f5e17]503 if (screenbuffer_init(&connections[i].screenbuffer,
504 fb_info.cols, fb_info.rows) == NULL) {
[c738d65]505 /* FIXME: handle error */
[3993b3d]506 return -1;
507 }
[79460ae]508 }
[d32af35]509 connections[KERNEL_CONSOLE].used = 1;
[51c1b003]510
[c738d65]511 interbuffer = mmap(NULL,
[01f5e17]512 sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
513 PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
[c738d65]514 if (!interbuffer) {
[215e375]515 if (ipc_share_out_start(fb_info.phone, interbuffer,
[27d293a]516 AS_AREA_READ) != EOK) {
[01f5e17]517 munmap(interbuffer,
518 sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
[390a678]519 interbuffer = NULL;
520 }
521 }
[3ad953c]522
[c738d65]523 curs_goto(0, 0);
[01f5e17]524 curs_visibility(
525 connections[active_console].screenbuffer.is_cursor_visible);
[3ad953c]526
[b1f51f0]527 /* Register at NS */
[271b540]528 if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
[51c1b003]529 return -1;
530
[3ad953c]531 /* Receive kernel notifications */
532 if (sysinfo_value("kconsole.present")) {
533 int devno = sysinfo_value("kconsole.devno");
534 int inr = sysinfo_value("kconsole.inr");
535 if (ipc_register_irq(inr, devno, 0, NULL) != EOK)
536 printf(NAME ": Error registering kconsole notifications\n");
537
538 async_set_interrupt_received(interrupt_received);
539 }
540
[271b540]541 // FIXME: avoid connectiong to itself, keep using klog
542 // printf(NAME ": Accepting connections\n");
[eaf34f7]543 async_manager();
[3ad953c]544
545 return 0;
[51c1b003]546}
[ce5bcb4]547
548/** @}
549 */
Note: See TracBrowser for help on using the repository browser.