source: mainline/uspace/srv/hid/console/console.c@ a40dea3

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

Eliminate devmap_obsolete API.

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