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

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

Hack to fix phone leakage in console

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