source: mainline/uspace/srv/hid/console/console.c@ 19f857a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 19f857a was 19f857a, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Rename string.h to str.h to avoid header conflict with standard C string.h.

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