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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc5908e was 103ae7f8, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Console looks for mice as well

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