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
RevLine 
[51c1b003]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[51c1b003]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 */
[ce5bcb4]28
[231a60a]29/** @addtogroup console
[1601f3c]30 * @{
[ce5bcb4]31 */
32/** @file
33 */
34
[3209923]35#include <libc.h>
[e795203]36#include <ipc/kbd.h>
[424cd43]37#include <io/keycode.h>
[9f51afc]38#include <ipc/mouse.h>
[79460ae]39#include <ipc/fb.h>
[51c1b003]40#include <ipc/services.h>
[007e6efa]41#include <ipc/ns.h>
[51c1b003]42#include <errno.h>
[30db06c]43#include <str_error.h>
[d3e6935]44#include <ipc/console.h>
[eaf34f7]45#include <unistd.h>
46#include <async.h>
[d9c8c81]47#include <adt/fifo.h>
[2def788]48#include <sys/mman.h>
[271b540]49#include <stdio.h>
[19f857a]50#include <str.h>
[3ad953c]51#include <sysinfo.h>
[05641a9e]52#include <event.h>
[424cd43]53#include <devmap.h>
[47a350f]54#include <fcntl.h>
55#include <vfs/vfs.h>
[1e4cada]56#include <fibril_synch.h>
[369a5f8]57#include <io/style.h>
58#include <io/screenbuffer.h>
[79460ae]59
[9805cde]60#include "console.h"
[e1c4849]61#include "gcons.h"
[cc1f8d4]62#include "keybuffer.h"
[369a5f8]63
[e1c4849]64
[1313ee9]65#define NAME "console"
66#define NAMESPACE "term"
[30db06c]67/** Interval for checking for new keyboard (1/4s). */
68#define HOTPLUG_WATCH_INTERVAL (1000 * 250)
[eaf34f7]69
[ccd1a14]70/** Phone to the keyboard driver. */
71static int kbd_phone;
72
[9f51afc]73/** Phone to the mouse driver. */
74static int mouse_phone;
75
[dc033a1]76/** Information about framebuffer */
[3993b3d]77struct {
[9f1362d4]78 int phone; /**< Framebuffer phone */
[96b02eb9]79 sysarg_t cols; /**< Framebuffer columns */
80 sysarg_t rows; /**< Framebuffer rows */
81 sysarg_t color_cap; /**< Color capabilities (FB_CCAP_xxx) */
[3993b3d]82} fb_info;
83
[79460ae]84typedef struct {
[424cd43]85 size_t index; /**< Console index */
86 size_t refcount; /**< Connection reference count */
[991f645]87 devmap_handle_t devmap_handle; /**< Device handle */
[424cd43]88 keybuffer_t keybuffer; /**< Buffer for incoming keys. */
89 screenbuffer_t scr; /**< Screenbuffer for saving screen
90 contents and related settings. */
91} console_t;
[79460ae]92
[1601f3c]93/** Array of data for virtual consoles */
[424cd43]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];
[1601f3c]99
100/** Pointer to memory shared with framebufer used for
101 faster virtual console switching */
102static keyfield_t *interbuffer = NULL;
[d2cc7e1]103
[dc033a1]104/** Information on row-span yet unsent to FB driver. */
105struct {
[96b02eb9]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. */
[dc033a1]109} fb_pending;
[d2cc7e1]110
[953769f]111static FIBRIL_MUTEX_INITIALIZE(input_mutex);
112static FIBRIL_CONDVAR_INITIALIZE(input_cv);
[429acb9]113
[8c6337d]114static void curs_visibility(bool visible)
[429acb9]115{
[8c6337d]116 async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
117}
118
119static void curs_hide_sync(void)
120{
[ffa2c8ef]121 async_req_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
[429acb9]122}
123
[96b02eb9]124static void curs_goto(sysarg_t x, sysarg_t y)
[429acb9]125{
[424cd43]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);
[429acb9]132}
133
[ebfabf6]134static void screen_yield(void)
[10270a8]135{
[ffa2c8ef]136 async_req_0_0(fb_info.phone, FB_SCREEN_YIELD);
[10270a8]137}
138
[ebfabf6]139static void screen_reclaim(void)
[10270a8]140{
[ffa2c8ef]141 async_req_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
[10270a8]142}
143
[ccd1a14]144static void kbd_yield(void)
145{
[ffa2c8ef]146 async_req_0_0(kbd_phone, KBD_YIELD);
[ccd1a14]147}
148
149static void kbd_reclaim(void)
150{
[ffa2c8ef]151 async_req_0_0(kbd_phone, KBD_RECLAIM);
[ccd1a14]152}
153
[9f1362d4]154static void set_style(uint8_t style)
[429acb9]155{
[424cd43]156 async_msg_1(fb_info.phone, FB_SET_STYLE, style);
[429acb9]157}
158
[9f1362d4]159static void set_color(uint8_t fgcolor, uint8_t bgcolor, uint8_t flags)
[429acb9]160{
[9805cde]161 async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
162}
163
[9f1362d4]164static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor)
[9805cde]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 }
[429acb9]183}
184
[96b02eb9]185static int ccap_fb_to_con(sysarg_t ccap_fb, sysarg_t *ccap_con)
[50cfa6c]186{
187 switch (ccap_fb) {
[9f1362d4]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;
[50cfa6c]202 }
[9f1362d4]203
[50cfa6c]204 return EOK;
205}
206
[dc033a1]207/** Send an area of screenbuffer to the FB driver. */
[96b02eb9]208static void fb_update_area(console_t *cons, sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height)
[d2cc7e1]209{
[dc033a1]210 if (interbuffer) {
[96b02eb9]211 sysarg_t x;
212 sysarg_t y;
[424cd43]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);
[dc033a1]218 }
219 }
[1601f3c]220
221 async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]222 x0, y0, width, height);
[dc033a1]223 }
[d2cc7e1]224}
225
[dc033a1]226/** Flush pending cells to FB. */
227static void fb_pending_flush(void)
[d2cc7e1]228{
[1601f3c]229 if (fb_pending.cnt > 0) {
[424cd43]230 fb_update_area(active_console, fb_pending.col,
[1601f3c]231 fb_pending.row, fb_pending.cnt, 1);
232 fb_pending.cnt = 0;
[d2cc7e1]233 }
234}
235
[dc033a1]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.
[1601f3c]240 *
[dc033a1]241 */
[96b02eb9]242static void cell_mark_changed(sysarg_t col, sysarg_t row)
[429acb9]243{
[1601f3c]244 if (fb_pending.cnt != 0) {
[424cd43]245 if ((col != fb_pending.col + fb_pending.cnt)
246 || (row != fb_pending.row)) {
[dc033a1]247 fb_pending_flush();
248 }
249 }
[1601f3c]250
251 if (fb_pending.cnt == 0) {
[dc033a1]252 fb_pending.col = col;
[424cd43]253 fb_pending.row = row;
[d2cc7e1]254 }
[1601f3c]255
256 fb_pending.cnt++;
[429acb9]257}
258
[dc033a1]259/** Print a character to the active VC with buffering. */
[96b02eb9]260static void fb_putchar(wchar_t c, sysarg_t col, sysarg_t row)
[dc033a1]261{
[424cd43]262 async_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
[dc033a1]263}
264
265/** Process a character from the client (TTY emulation). */
[424cd43]266static void write_char(console_t *cons, wchar_t ch)
[10569b1]267{
[0ed2e0e]268 bool flush_cursor = false;
[9f1362d4]269
[7ce3cb2]270 switch (ch) {
[00bb6965]271 case '\n':
[dc033a1]272 fb_pending_flush();
[0ed2e0e]273 flush_cursor = true;
[424cd43]274 cons->scr.position_y++;
275 cons->scr.position_x = 0;
[00bb6965]276 break;
277 case '\r':
278 break;
279 case '\t':
[424cd43]280 cons->scr.position_x += 8;
281 cons->scr.position_x -= cons->scr.position_x % 8;
[00bb6965]282 break;
283 case '\b':
[424cd43]284 if (cons->scr.position_x == 0)
[10569b1]285 break;
[424cd43]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, ' ');
[00bb6965]290 break;
[1601f3c]291 default:
[424cd43]292 if (cons == active_console)
293 cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
[1601f3c]294
[424cd43]295 screenbuffer_putchar(&cons->scr, ch);
296 cons->scr.position_x++;
[10569b1]297 }
[1601f3c]298
[0ed2e0e]299 if (cons->scr.position_x >= cons->scr.size_x) {
300 flush_cursor = true;
[424cd43]301 cons->scr.position_y++;
[0ed2e0e]302 }
[10569b1]303
[424cd43]304 if (cons->scr.position_y >= cons->scr.size_y) {
[dc033a1]305 fb_pending_flush();
[424cd43]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)
[0cc4313]311 async_msg_1(fb_info.phone, FB_SCROLL, 1);
[10569b1]312 }
[9f1362d4]313
[0ed2e0e]314 if (cons == active_console && flush_cursor)
315 curs_goto(cons->scr.position_x, cons->scr.position_y);
[424cd43]316 cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
[10569b1]317}
318
[429acb9]319/** Switch to new console */
[424cd43]320static void change_console(console_t *cons)
[429acb9]321{
[700af62]322 if (cons == active_console) {
[429acb9]323 return;
[700af62]324 }
[1601f3c]325
[dc033a1]326 fb_pending_flush();
[1601f3c]327
[424cd43]328 if (cons == kernel_console) {
[1f83244]329 async_serialize_start();
[8c6337d]330 curs_hide_sync();
[76fca31]331 gcons_in_kernel();
[ebfabf6]332 screen_yield();
[ccd1a14]333 kbd_yield();
[1f83244]334 async_serialize_end();
[1601f3c]335
[3ad953c]336 if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
337 prev_console = active_console;
[424cd43]338 active_console = kernel_console;
[3ad953c]339 } else
[424cd43]340 cons = active_console;
[01f5e17]341 }
[6d5005c]342
[424cd43]343 if (cons != kernel_console) {
[76fca31]344 async_serialize_start();
345
[424cd43]346 if (active_console == kernel_console) {
[ebfabf6]347 screen_reclaim();
[ccd1a14]348 kbd_reclaim();
[76fca31]349 gcons_redraw_console();
[10270a8]350 }
[76fca31]351
[424cd43]352 active_console = cons;
353 gcons_change_console(cons->index);
[76fca31]354
[424cd43]355 set_attrs(&cons->scr.attrs);
[8c6337d]356 curs_visibility(false);
[9f1362d4]357
[96b02eb9]358 sysarg_t x;
359 sysarg_t y;
[9f1362d4]360 int rc = 0;
361
[76fca31]362 if (interbuffer) {
[424cd43]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);
[76fca31]367 }
[dc033a1]368 }
[424cd43]369
[76fca31]370 /* This call can preempt, but we are already at the end */
[dc033a1]371 rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]372 0, 0, cons->scr.size_x,
373 cons->scr.size_y);
[76fca31]374 }
375
376 if ((!interbuffer) || (rc != 0)) {
[424cd43]377 set_attrs(&cons->scr.attrs);
378 screen_clear();
[76fca31]379
[424cd43]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))
[9805cde]385 set_attrs(&field->attrs);
[424cd43]386
387 cons->scr.attrs = field->attrs;
[76fca31]388 if ((field->character == ' ') &&
[424cd43]389 (attrs_same(field->attrs, cons->scr.attrs)))
[76fca31]390 continue;
[1601f3c]391
[424cd43]392 fb_putchar(field->character, x, y);
[76fca31]393 }
394 }
395
[424cd43]396 curs_goto(cons->scr.position_x, cons->scr.position_y);
397 curs_visibility(cons->scr.is_cursor_visible);
[76fca31]398
399 async_serialize_end();
[429acb9]400 }
401}
[10569b1]402
[e87e18f]403/** Handler for keyboard */
[eaf34f7]404static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
[51c1b003]405{
[424cd43]406 /* Ignore parameters, the connection is already opened */
[1601f3c]407 while (true) {
[424cd43]408 ipc_call_t call;
409 ipc_callid_t callid = async_get_call(&call);
410
411 int retval;
412 console_event_t ev;
413
[228e490]414 switch (IPC_GET_IMETHOD(call)) {
[eaf34f7]415 case IPC_M_PHONE_HUNGUP:
416 /* TODO: Handle hangup */
417 return;
[fa09449]418 case KBD_EVENT:
419 /* Got event from keyboard driver. */
[eaf34f7]420 retval = 0;
[fa09449]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
[f89979b]426 if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
[0175246]427 CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
[424cd43]428 if (ev.key == KC_F1 + KERNEL_CONSOLE)
429 change_console(kernel_console);
[429acb9]430 else
[424cd43]431 change_console(&consoles[ev.key - KC_F1]);
[eaf34f7]432 break;
433 }
[cf28036c]434
[953769f]435 fibril_mutex_lock(&input_mutex);
[424cd43]436 keybuffer_push(&active_console->keybuffer, &ev);
[af65b72]437 fibril_condvar_broadcast(&input_cv);
[953769f]438 fibril_mutex_unlock(&input_mutex);
[eaf34f7]439 break;
440 default:
[1029d3d3]441 retval = ENOENT;
[085bd54]442 }
[ffa2c8ef]443 async_answer_0(callid, retval);
[eaf34f7]444 }
445}
446
[9f51afc]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);
[9f1362d4]454
[9f51afc]455 int retval;
[9f1362d4]456
[228e490]457 switch (IPC_GET_IMETHOD(call)) {
[9f51afc]458 case IPC_M_PHONE_HUNGUP:
459 /* TODO: Handle hangup */
460 return;
461 case MEVENT_BUTTON:
[9f1362d4]462 if (IPC_GET_ARG1(call) == 1) {
463 int newcon = gcons_mouse_btn((bool) IPC_GET_ARG2(call));
[700af62]464 if (newcon != -1) {
[9f51afc]465 change_console(&consoles[newcon]);
[700af62]466 }
[9f51afc]467 }
468 retval = 0;
469 break;
470 case MEVENT_MOVE:
[9f1362d4]471 gcons_mouse_move((int) IPC_GET_ARG1(call),
472 (int) IPC_GET_ARG2(call));
[9f51afc]473 retval = 0;
474 break;
475 default:
476 retval = ENOENT;
477 }
478
[ffa2c8ef]479 async_answer_0(callid, retval);
[9f51afc]480 }
481}
482
[424cd43]483static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
[d2cc7e1]484{
[472c09d]485 void *buf;
[171f9a1]486 size_t size;
[eda925a]487 int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
[1601f3c]488
[472c09d]489 if (rc != EOK) {
[ffa2c8ef]490 async_answer_0(rid, rc);
[424cd43]491 return;
492 }
[1601f3c]493
[6568225]494 async_serialize_start();
[1601f3c]495
[424cd43]496 size_t off = 0;
[171f9a1]497 while (off < size) {
[424cd43]498 wchar_t ch = str_decode(buf, &off, size);
499 write_char(cons, ch);
[d2cc7e1]500 }
[1601f3c]501
[6568225]502 async_serialize_end();
[1601f3c]503
[424cd43]504 gcons_notify_char(cons->index);
[ffa2c8ef]505 async_answer_1(rid, EOK, size);
[424cd43]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;
[0da4e41]514 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]515 async_answer_0(callid, EINVAL);
516 async_answer_0(rid, EINVAL);
[424cd43]517 return;
518 }
519
520 char *buf = (char *) malloc(size);
521 if (buf == NULL) {
[ffa2c8ef]522 async_answer_0(callid, ENOMEM);
523 async_answer_0(rid, ENOMEM);
[424cd43]524 return;
525 }
526
527 size_t pos = 0;
528 console_event_t ev;
[953769f]529 fibril_mutex_lock(&input_mutex);
[9f1362d4]530
[af65b72]531recheck:
[424cd43]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) {
[0da4e41]540 (void) async_data_read_finalize(callid, buf, size);
[ffa2c8ef]541 async_answer_1(rid, EOK, size);
[424cd43]542 free(buf);
543 } else {
[af65b72]544 fibril_condvar_wait(&input_cv, &input_mutex);
545 goto recheck;
[424cd43]546 }
[9f1362d4]547
[953769f]548 fibril_mutex_unlock(&input_mutex);
[424cd43]549}
550
551static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
552{
553 console_event_t ev;
[9f1362d4]554
[953769f]555 fibril_mutex_lock(&input_mutex);
[9f1362d4]556
[af65b72]557recheck:
[424cd43]558 if (keybuffer_pop(&cons->keybuffer, &ev)) {
[ffa2c8ef]559 async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
[424cd43]560 } else {
[af65b72]561 fibril_condvar_wait(&input_cv, &input_mutex);
562 goto recheck;
[424cd43]563 }
[9f1362d4]564
[953769f]565 fibril_mutex_unlock(&input_mutex);
[d2cc7e1]566}
567
[eaf34f7]568/** Default thread for new connections */
[b1f51f0]569static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
[eaf34f7]570{
[424cd43]571 console_t *cons = NULL;
[3ad953c]572
[424cd43]573 size_t i;
574 for (i = 0; i < CONSOLE_COUNT; i++) {
575 if (i == KERNEL_CONSOLE)
576 continue;
577
[991f645]578 if (consoles[i].devmap_handle == (devmap_handle_t) IPC_GET_ARG1(*icall)) {
[424cd43]579 cons = &consoles[i];
580 break;
581 }
582 }
583
584 if (cons == NULL) {
[ffa2c8ef]585 async_answer_0(iid, ENOENT);
[eaf34f7]586 return;
587 }
[424cd43]588
589 ipc_callid_t callid;
590 ipc_call_t call;
[96b02eb9]591 sysarg_t arg1;
592 sysarg_t arg2;
593 sysarg_t arg3;
[9f1362d4]594
[50cfa6c]595 int rc;
[a7d2d78]596
[085bd54]597 async_serialize_start();
[424cd43]598 if (cons->refcount == 0)
599 gcons_notify_connect(cons->index);
600
601 cons->refcount++;
[10569b1]602
[eaf34f7]603 /* Accept the connection */
[ffa2c8ef]604 async_answer_0(iid, EOK);
[3ad953c]605
[1601f3c]606 while (true) {
[085bd54]607 async_serialize_end();
[eaf34f7]608 callid = async_get_call(&call);
[085bd54]609 async_serialize_start();
[3ad953c]610
[96858e8]611 arg1 = 0;
612 arg2 = 0;
[fa09449]613 arg3 = 0;
[1601f3c]614
[228e490]615 switch (IPC_GET_IMETHOD(call)) {
[eaf34f7]616 case IPC_M_PHONE_HUNGUP:
[424cd43]617 cons->refcount--;
618 if (cons->refcount == 0)
619 gcons_notify_disconnect(cons->index);
[eaf34f7]620 return;
[4198f9c3]621 case VFS_OUT_READ:
[424cd43]622 async_serialize_end();
623 cons_read(cons, callid, &call);
624 async_serialize_start();
625 continue;
[4198f9c3]626 case VFS_OUT_WRITE:
[6568225]627 async_serialize_end();
[424cd43]628 cons_write(cons, callid, &call);
[6568225]629 async_serialize_start();
[d2cc7e1]630 continue;
[4198f9c3]631 case VFS_OUT_SYNC:
[424cd43]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;
[ad123964]638 case CONSOLE_CLEAR:
[3993b3d]639 /* Send message to fb */
[424cd43]640 if (cons == active_console)
641 async_msg_0(fb_info.phone, FB_CLEAR);
[3993b3d]642
[424cd43]643 screenbuffer_clear(&cons->scr);
[3993b3d]644
[eaf34f7]645 break;
[ad123964]646 case CONSOLE_GOTO:
[424cd43]647 screenbuffer_goto(&cons->scr,
648 IPC_GET_ARG1(call), IPC_GET_ARG2(call));
649 if (cons == active_console)
[00bb6965]650 curs_goto(IPC_GET_ARG1(call),
[01f5e17]651 IPC_GET_ARG2(call));
[ad123964]652 break;
[19528516]653 case CONSOLE_GET_POS:
654 arg1 = cons->scr.position_x;
655 arg2 = cons->scr.position_y;
656 break;
[424cd43]657 case CONSOLE_GET_SIZE:
658 arg1 = fb_info.cols;
659 arg2 = fb_info.rows;
[a9bd960c]660 break;
[50cfa6c]661 case CONSOLE_GET_COLOR_CAP:
[9f1362d4]662 rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
[50cfa6c]663 if (rc != EOK) {
[ffa2c8ef]664 async_answer_0(callid, rc);
[50cfa6c]665 continue;
666 }
667 break;
[a9bd960c]668 case CONSOLE_SET_STYLE:
[dc033a1]669 fb_pending_flush();
[9805cde]670 arg1 = IPC_GET_ARG1(call);
[424cd43]671 screenbuffer_set_style(&cons->scr, arg1);
672 if (cons == active_console)
[9805cde]673 set_style(arg1);
674 break;
675 case CONSOLE_SET_COLOR:
[dc033a1]676 fb_pending_flush();
[9805cde]677 arg1 = IPC_GET_ARG1(call);
678 arg2 = IPC_GET_ARG2(call);
679 arg3 = IPC_GET_ARG3(call);
[424cd43]680 screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
681 if (cons == active_console)
[9805cde]682 set_color(arg1, arg2, arg3);
683 break;
684 case CONSOLE_SET_RGB_COLOR:
[dc033a1]685 fb_pending_flush();
[a9bd960c]686 arg1 = IPC_GET_ARG1(call);
687 arg2 = IPC_GET_ARG2(call);
[424cd43]688 screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
689 if (cons == active_console)
[9805cde]690 set_rgb_color(arg1, arg2);
[0c6984e]691 break;
[a8b2b5b2]692 case CONSOLE_CURSOR_VISIBILITY:
[dc033a1]693 fb_pending_flush();
[a8b2b5b2]694 arg1 = IPC_GET_ARG1(call);
[424cd43]695 cons->scr.is_cursor_visible = arg1;
696 if (cons == active_console)
[a8b2b5b2]697 curs_visibility(arg1);
698 break;
[424cd43]699 case CONSOLE_GET_EVENT:
700 async_serialize_end();
701 cons_get_event(cons, callid, &call);
702 async_serialize_start();
703 continue;
[3fe00ee]704 case CONSOLE_KCON_ENABLE:
[424cd43]705 change_console(kernel_console);
[3fe00ee]706 break;
[eaf34f7]707 }
[ffa2c8ef]708 async_answer_3(callid, EOK, arg1, arg2, arg3);
[eaf34f7]709 }
710}
711
[3ad953c]712static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
713{
714 change_console(prev_console);
715}
716
[103ae7f8]717static int connect_keyboard_or_mouse(const char *devname,
718 async_client_conn_t handler, const char *path)
[eaf34f7]719{
[700af62]720 int fd = open(path, O_RDONLY);
721 if (fd < 0) {
722 return fd;
[47a350f]723 }
[9f1362d4]724
[700af62]725 int phone = fd_phone(fd);
726 if (phone < 0) {
[47a350f]727 printf(NAME ": Failed to connect to input device\n");
[700af62]728 return phone;
[00bb6965]729 }
[9f1362d4]730
[103ae7f8]731 int rc = async_connect_to_me(phone, SERVICE_CONSOLE, 0, 0, handler);
[700af62]732 if (rc != EOK) {
[30db06c]733 printf(NAME ": " \
734 "Failed to create callback from input device: %s.\n",
735 str_error(rc));
[700af62]736 return rc;
[4904de8]737 }
[9f1362d4]738
[103ae7f8]739 printf(NAME ": found %s \"%s\".\n", devname, path);
[700af62]740
741 return phone;
742}
743
[103ae7f8]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};
[30db06c]758
759/** Periodically check for new keyboards in /dev/class/.
[f03d3786]760 *
[30db06c]761 * @param arg Class name.
762 * @return This function should never exit.
[f03d3786]763 */
[103ae7f8]764static int check_new_device_fibril(void *arg)
[700af62]765{
[103ae7f8]766 struct hid_class_info *dev_info = arg;
[700af62]767
[30db06c]768 size_t index = 1;
[700af62]769
770 while (true) {
[30db06c]771 async_usleep(HOTPLUG_WATCH_INTERVAL);
[700af62]772 char *path;
[30db06c]773 int rc = asprintf(&path, "/dev/class/%s\\%zu",
[103ae7f8]774 dev_info->classname, index);
[700af62]775 if (rc < 0) {
776 continue;
777 }
778 rc = 0;
[103ae7f8]779 rc = dev_info->connection_func(path);
[700af62]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 */
[103ae7f8]794static void check_new_devices_in_background(int (*connection_func)(const char *),
795 const char *classname)
[700af62]796{
[103ae7f8]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);
[700af62]814 if (!fid) {
[103ae7f8]815 printf(NAME
816 ": failed to create hot-plug-watch fibril for %s.\n",
817 classname);
[700af62]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
[103ae7f8]831 mouse_phone = connect_mouse("/dev/hid_in/mouse");
[9f51afc]832 if (mouse_phone < 0) {
[103ae7f8]833 printf(NAME ": Failed to connect to mouse device: %s.\n",
834 str_error(mouse_phone));
[9f51afc]835 }
[9f1362d4]836
[51c1b003]837 /* Connect to framebuffer driver */
[007e6efa]838 fb_info.phone = service_connect_blocking(SERVICE_VIDEO, 0, 0);
[4904de8]839 if (fb_info.phone < 0) {
840 printf(NAME ": Failed to connect to video service\n");
841 return -1;
[3993b3d]842 }
[9f1362d4]843
[424cd43]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 }
[516ff92]850
[e1c4849]851 /* Initialize gcons */
852 gcons_init(fb_info.phone);
[3993b3d]853
[424cd43]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);
[9f1362d4]857 async_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
[1601f3c]858
[7447572]859 /* Set up shared memory buffer. */
[424cd43]860 size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
[7447572]861 interbuffer = as_get_mappable_page(ib_size);
[1601f3c]862
[7447572]863 if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
[424cd43]864 AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
[7447572]865 interbuffer = NULL;
[1601f3c]866
[7447572]867 if (interbuffer) {
[0da4e41]868 if (async_share_out_start(fb_info.phone, interbuffer,
[27d293a]869 AS_AREA_READ) != EOK) {
[7447572]870 as_area_destroy(interbuffer);
[390a678]871 interbuffer = NULL;
872 }
873 }
[3ad953c]874
[424cd43]875 fb_pending.cnt = 0;
[3ad953c]876
[424cd43]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) {
[7e752b2]883 printf(NAME ": Unable to allocate screen buffer %zu\n", i);
[424cd43]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
[1313ee9]891 char vc[DEVMAP_NAME_MAXLEN + 1];
[7e752b2]892 snprintf(vc, DEVMAP_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
[424cd43]893
[991f645]894 if (devmap_device_register(vc, &consoles[i].devmap_handle) != EOK) {
[424cd43]895 printf(NAME ": Unable to register device %s\n", vc);
896 return false;
897 }
898 }
899 }
900
[1035437]901 /* Disable kernel output to the console */
902 __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
903
[424cd43]904 /* Initialize the screen */
[953769f]905 async_serialize_start();
[1035437]906 gcons_redraw_console();
[369a5f8]907 set_style(STYLE_NORMAL);
[424cd43]908 screen_clear();
909 curs_goto(0, 0);
910 curs_visibility(active_console->scr.is_cursor_visible);
[953769f]911 async_serialize_end();
[51c1b003]912
[3ad953c]913 /* Receive kernel notifications */
[007e6efa]914 async_set_interrupt_received(interrupt_received);
[05641a9e]915 if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
916 printf(NAME ": Error registering kconsole notifications\n");
[1601f3c]917
[700af62]918 /* Start fibril for checking on hot-plugged keyboards. */
[103ae7f8]919 check_new_devices_in_background(connect_keyboard, "keyboard");
920 check_new_devices_in_background(connect_mouse, "mouse");
[700af62]921
[424cd43]922 return true;
923}
924
[47a350f]925static void usage(void)
926{
927 printf("Usage: console <input>\n");
928}
929
[424cd43]930int main(int argc, char *argv[])
931{
[47a350f]932 if (argc < 2) {
933 usage();
934 return -1;
935 }
936
[424cd43]937 printf(NAME ": HelenOS Console service\n");
938
[47a350f]939 if (!console_init(argv[1]))
[424cd43]940 return -1;
[700af62]941
[424cd43]942 printf(NAME ": Accepting connections\n");
[eaf34f7]943 async_manager();
[3ad953c]944
945 return 0;
[51c1b003]946}
[516ff92]947
[ce5bcb4]948/** @}
949 */
Note: See TracBrowser for help on using the repository browser.