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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fa23560 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
Line 
1/*
2 * Copyright (c) 2006 Josef Cejka
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 */
28
29/** @addtogroup console
30 * @{
31 */
32/** @file
33 */
34
35/* TODO: remove */
36#include <stdio.h>
37
38#include <fb.h>
39#include <ipc/ipc.h>
40#include <keys.h>
41#include <ipc/fb.h>
42#include <ipc/services.h>
43#include <errno.h>
44#include <key_buffer.h>
45#include <console.h>
46#include <unistd.h>
47#include <async.h>
48#include <libadt/fifo.h>
49#include <screenbuffer.h>
50#include <sys/mman.h>
51
52#include "gcons.h"
53
54#define MAX_KEYREQUESTS_BUFFERED 32
55
56#define NAME "CONSOLE"
57
58/** Index of currently used virtual console.
59 */
60int active_console = 0;
61
62/** Information about framebuffer
63 */
64struct {
65 int phone; /**< Framebuffer phone */
66 ipcarg_t rows; /**< Framebuffer rows */
67 ipcarg_t cols; /**< Framebuffer columns */
68} fb_info;
69
70typedef struct {
71 keybuffer_t keybuffer; /**< Buffer for incoming keys. */
72 /** Buffer for unsatisfied request for keys. */
73 FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
74 MAX_KEYREQUESTS_BUFFERED);
75 int keyrequest_counter; /**< Number of requests in buffer. */
76 int client_phone; /**< Phone to connected client. */
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. */
81} connection_t;
82
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
88 * switching */
89
90static int kernel_pixmap = -1; /**< Number of fb pixmap, where kernel
91 * console is stored */
92
93
94/** Find unused virtual console.
95 *
96 */
97static int find_free_connection(void)
98{
99 int i;
100
101 for (i = 0; i < CONSOLE_COUNT; i++) {
102 if (!connections[i].used)
103 return i;
104 }
105 return -1;
106}
107
108static void clrscr(void)
109{
110 async_msg(fb_info.phone, FB_CLEAR, 0);
111}
112
113static void curs_visibility(int v)
114{
115 async_msg(fb_info.phone, FB_CURSOR_VISIBILITY, v);
116}
117
118static void curs_goto(int row, int col)
119{
120 async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
121}
122
123static void set_style(style_t *style)
124{
125 async_msg_2(fb_info.phone, FB_SET_STYLE, style->fg_color,
126 style->bg_color);
127}
128
129static void set_style_col(int fgcolor, int bgcolor)
130{
131 async_msg_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
132}
133
134static void prtchr(char c, int row, int col)
135{
136 async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
137}
138
139/** Check key and process special keys.
140 *
141 *
142 */
143static void write_char(int console, char key)
144{
145 screenbuffer_t *scr = &(connections[console].screenbuffer);
146
147 switch (key) {
148 case '\n':
149 scr->position_y++;
150 scr->position_x = 0;
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)
160 break;
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);
169
170 screenbuffer_putchar(scr, key);
171 scr->position_x++;
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;
178 screenbuffer_clear_line(scr, scr->top_line);
179 scr->top_line = (scr->top_line + 1) % scr->size_y;
180 if (console == active_console)
181 async_msg(fb_info.phone, FB_SCROLL, 1);
182 }
183
184 scr->position_x = scr->position_x % scr->size_x;
185
186 if (console == active_console)
187 curs_goto(scr->position_y, scr->position_x);
188
189}
190
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 */
201 newpmap = async_req(fb_info.phone, FB_VP2PIXMAP, 0, NULL);
202 if (newpmap < 0)
203 return -1;
204
205 if (oldpixmap != -1) {
206 /* Show old screen */
207 async_msg_2(fb_info.phone, FB_VP_DRAW_PIXMAP, 0, oldpixmap);
208 /* Drop old pixmap */
209 async_msg(fb_info.phone, FB_DROP_PIXMAP, oldpixmap);
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;
220 int i, j, rc;
221 keyfield_t *field;
222 style_t *style;
223
224 if (newcons == active_console)
225 return;
226
227 if (newcons == KERNEL_CONSOLE) {
228 if (active_console == KERNEL_CONSOLE)
229 return;
230 active_console = KERNEL_CONSOLE;
231 curs_visibility(0);
232
233 async_serialize_start();
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 }
243 async_serialize_end();
244
245 __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
246 return;
247 }
248
249 async_serialize_start();
250
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
259 set_style(&conn->screenbuffer.style);
260 curs_visibility(0);
261 if (interbuffer) {
262 for (i = 0; i < conn->screenbuffer.size_x; i++)
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 }
270 /* This call can preempt, but we are already at the end */
271 rc = async_req_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL,
272 NULL);
273 }
274
275 if ((!interbuffer) || (rc != 0)) {
276 set_style(&conn->screenbuffer.style);
277 clrscr();
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++) {
282 field = get_field_at(&conn->screenbuffer, i,
283 j);
284 if (!style_same(*style, field->style))
285 set_style(&field->style);
286 style = &field->style;
287 if ((field->character == ' ') &&
288 (style_same(field->style,
289 conn->screenbuffer.style)))
290 continue;
291
292 prtchr(field->character, j, i);
293 }
294 }
295
296 curs_goto(conn->screenbuffer.position_y,
297 conn->screenbuffer.position_x);
298 curs_visibility(conn->screenbuffer.is_cursor_visible);
299
300 async_serialize_end();
301}
302
303/** Handler for keyboard */
304static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
305{
306 ipc_callid_t callid;
307 ipc_call_t call;
308 int retval;
309 int c;
310 connection_t *conn;
311 int newcon;
312
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;
320 case KBD_MS_LEFT:
321 newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
322 if (newcon != -1)
323 change_console(newcon);
324 retval = 0;
325 break;
326 case KBD_MS_MOVE:
327 gcons_mouse_move(IPC_GET_ARG1(call),
328 IPC_GET_ARG2(call));
329 retval = 0;
330 break;
331 case KBD_PUSHCHAR:
332 /* got key from keyboard driver */
333
334 retval = 0;
335 c = IPC_GET_ARG1(call);
336 /* switch to another virtual console */
337
338 conn = &connections[active_console];
339/*
340 * if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
341 * CONSOLE_COUNT)) {
342 */
343 if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
344 if (c == 0x112)
345 change_console(KERNEL_CONSOLE);
346 else
347 change_console(c - 0x101);
348 break;
349 }
350
351 /* if client is awaiting key, send it */
352 if (conn->keyrequest_counter > 0) {
353 conn->keyrequest_counter--;
354 ipc_answer_fast(fifo_pop(conn->keyrequests), 0,
355 c, 0);
356 break;
357 }
358
359 keybuffer_push(&conn->keybuffer, c);
360 retval = 0;
361
362 break;
363 default:
364 retval = ENOENT;
365 }
366 ipc_answer_fast(callid, retval, 0, 0);
367 }
368}
369
370/** Default thread for new connections */
371static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
372{
373 ipc_callid_t callid;
374 ipc_call_t call;
375 int consnum;
376 ipcarg_t arg1, arg2;
377 connection_t *conn;
378
379 if ((consnum = find_free_connection()) == -1) {
380 ipc_answer_fast(iid, ELIMIT, 0, 0);
381 return;
382 }
383 conn = &connections[consnum];
384 conn->used = 1;
385
386 async_serialize_start();
387 gcons_notify_connect(consnum);
388 conn->client_phone = IPC_GET_ARG3(call);
389 screenbuffer_clear(&conn->screenbuffer);
390
391 /* Accept the connection */
392 ipc_answer_fast(iid, 0, 0, 0);
393
394 while (1) {
395 async_serialize_end();
396 callid = async_get_call(&call);
397 async_serialize_start();
398
399 arg1 = 0;
400 arg2 = 0;
401 switch (IPC_GET_METHOD(call)) {
402 case IPC_M_PHONE_HUNGUP:
403 gcons_notify_disconnect(consnum);
404
405 /* Answer all pending requests */
406 while (conn->keyrequest_counter > 0) {
407 conn->keyrequest_counter--;
408 ipc_answer_fast(fifo_pop(conn->keyrequests),
409 ENOENT, 0, 0);
410 break;
411 }
412 conn->used = 0;
413 return;
414 case CONSOLE_PUTCHAR:
415 write_char(consnum, IPC_GET_ARG1(call));
416 gcons_notify_char(consnum);
417 break;
418 case CONSOLE_CLEAR:
419 /* Send message to fb */
420 if (consnum == active_console) {
421 async_msg(fb_info.phone, FB_CLEAR, 0);
422 }
423
424 screenbuffer_clear(&conn->screenbuffer);
425
426 break;
427 case CONSOLE_GOTO:
428 screenbuffer_goto(&conn->screenbuffer,
429 IPC_GET_ARG2(call), IPC_GET_ARG1(call));
430 if (consnum == active_console)
431 curs_goto(IPC_GET_ARG1(call),
432 IPC_GET_ARG2(call));
433 break;
434 case CONSOLE_GETSIZE:
435 arg1 = fb_info.rows;
436 arg2 = fb_info.cols;
437 break;
438 case CONSOLE_FLUSH:
439 if (consnum == active_console)
440 async_req_2(fb_info.phone, FB_FLUSH, 0, 0,
441 NULL, NULL);
442 break;
443 case CONSOLE_SET_STYLE:
444 arg1 = IPC_GET_ARG1(call);
445 arg2 = IPC_GET_ARG2(call);
446 screenbuffer_set_style(&conn->screenbuffer, arg1,
447 arg2);
448 if (consnum == active_console)
449 set_style_col(arg1, arg2);
450 break;
451 case CONSOLE_CURSOR_VISIBILITY:
452 arg1 = IPC_GET_ARG1(call);
453 conn->screenbuffer.is_cursor_visible = arg1;
454 if (consnum == active_console)
455 curs_visibility(arg1);
456 break;
457 case CONSOLE_GETCHAR:
458 if (keybuffer_empty(&conn->keybuffer)) {
459 /* buffer is empty -> store request */
460 if (conn->keyrequest_counter <
461 MAX_KEYREQUESTS_BUFFERED) {
462 fifo_push(conn->keyrequests, callid);
463 conn->keyrequest_counter++;
464 } else {
465 /*
466 * No key available and too many
467 * requests => fail.
468 */
469 ipc_answer_fast(callid, ELIMIT, 0, 0);
470 }
471 continue;
472 }
473 keybuffer_pop(&conn->keybuffer, (int *) &arg1);
474 break;
475 }
476 ipc_answer_fast(callid, 0, arg1, arg2);
477 }
478}
479
480int main(int argc, char *argv[])
481{
482 ipcarg_t phonehash;
483 int kbd_phone;
484 int i;
485
486 async_set_client_connection(client_connection);
487
488 /* Connect to keyboard driver */
489
490 kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0);
491 while (kbd_phone < 0) {
492 usleep(10000);
493 kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0);
494 }
495
496 if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0)
497 return -1;
498 async_new_connection(phonehash, 0, NULL, keyboard_events);
499
500 /* Connect to framebuffer driver */
501
502 fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0);
503 while (fb_info.phone < 0) {
504 usleep(10000);
505 fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0);
506 }
507
508 /* Save old kernel screen */
509 kernel_pixmap = switch_screens(-1);
510
511 /* Initialize gcons */
512 gcons_init(fb_info.phone);
513 /* Synchronize, the gcons can have something in queue */
514 async_req(fb_info.phone, FB_FLUSH, 0, NULL);
515 /* Enable double buffering */
516 async_msg_2(fb_info.phone, FB_VIEWPORT_DB, (sysarg_t) -1, 1);
517
518 async_req_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &fb_info.rows,
519 &fb_info.cols);
520 set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
521 clrscr();
522
523 /* Init virtual consoles */
524 for (i = 0; i < CONSOLE_COUNT; i++) {
525 connections[i].used = 0;
526 keybuffer_init(&connections[i].keybuffer);
527
528 connections[i].keyrequests.head = 0;
529 connections[i].keyrequests.tail = 0;
530 connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
531 connections[i].keyrequest_counter = 0;
532
533 if (screenbuffer_init(&connections[i].screenbuffer,
534 fb_info.cols, fb_info.rows) == NULL) {
535 /* FIXME: handle error */
536 return -1;
537 }
538 }
539 connections[KERNEL_CONSOLE].used = 1;
540
541 interbuffer = mmap(NULL,
542 sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
543 PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
544 if (!interbuffer) {
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);
550 interbuffer = NULL;
551 }
552 }
553
554 curs_goto(0, 0);
555 curs_visibility(
556 connections[active_console].screenbuffer.is_cursor_visible);
557
558 /* Register at NS */
559 if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
560 return -1;
561 }
562
563 async_manager();
564
565 return 0;
566}
567
568/** @}
569 */
Note: See TracBrowser for help on using the repository browser.