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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e686744c was 96858e8, checked in by Jakub Jermar <jakub@…>, 18 years ago

Fix cstyle in console.c.

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