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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd7acda 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
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>
[a2a3763]59#include <inttypes.h>
[79460ae]60
[9805cde]61#include "console.h"
[e1c4849]62#include "gcons.h"
[cc1f8d4]63#include "keybuffer.h"
[369a5f8]64
[e1c4849]65
[1313ee9]66#define NAME "console"
67#define NAMESPACE "term"
[30db06c]68/** Interval for checking for new keyboard (1/4s). */
69#define HOTPLUG_WATCH_INTERVAL (1000 * 250)
[eaf34f7]70
[a2a3763]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
[ccd1a14]76/** Phone to the keyboard driver. */
77static int kbd_phone;
78
[9f51afc]79/** Phone to the mouse driver. */
80static int mouse_phone;
81
[dc033a1]82/** Information about framebuffer */
[3993b3d]83struct {
[9f1362d4]84 int phone; /**< Framebuffer phone */
[96b02eb9]85 sysarg_t cols; /**< Framebuffer columns */
86 sysarg_t rows; /**< Framebuffer rows */
87 sysarg_t color_cap; /**< Color capabilities (FB_CCAP_xxx) */
[3993b3d]88} fb_info;
89
[79460ae]90typedef struct {
[424cd43]91 size_t index; /**< Console index */
92 size_t refcount; /**< Connection reference count */
[991f645]93 devmap_handle_t devmap_handle; /**< Device handle */
[424cd43]94 keybuffer_t keybuffer; /**< Buffer for incoming keys. */
95 screenbuffer_t scr; /**< Screenbuffer for saving screen
96 contents and related settings. */
97} console_t;
[79460ae]98
[a2a3763]99
100
[1601f3c]101/** Array of data for virtual consoles */
[424cd43]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];
[1601f3c]107
108/** Pointer to memory shared with framebufer used for
109 faster virtual console switching */
110static keyfield_t *interbuffer = NULL;
[d2cc7e1]111
[dc033a1]112/** Information on row-span yet unsent to FB driver. */
113struct {
[96b02eb9]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. */
[dc033a1]117} fb_pending;
[d2cc7e1]118
[953769f]119static FIBRIL_MUTEX_INITIALIZE(input_mutex);
120static FIBRIL_CONDVAR_INITIALIZE(input_cv);
[429acb9]121
[8c6337d]122static void curs_visibility(bool visible)
[429acb9]123{
[8c6337d]124 async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
125}
126
127static void curs_hide_sync(void)
128{
[ffa2c8ef]129 async_req_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
[429acb9]130}
131
[96b02eb9]132static void curs_goto(sysarg_t x, sysarg_t y)
[429acb9]133{
[424cd43]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);
[429acb9]140}
141
[ebfabf6]142static void screen_yield(void)
[10270a8]143{
[ffa2c8ef]144 async_req_0_0(fb_info.phone, FB_SCREEN_YIELD);
[10270a8]145}
146
[ebfabf6]147static void screen_reclaim(void)
[10270a8]148{
[ffa2c8ef]149 async_req_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
[10270a8]150}
151
[ccd1a14]152static void kbd_yield(void)
153{
[ffa2c8ef]154 async_req_0_0(kbd_phone, KBD_YIELD);
[ccd1a14]155}
156
157static void kbd_reclaim(void)
158{
[ffa2c8ef]159 async_req_0_0(kbd_phone, KBD_RECLAIM);
[ccd1a14]160}
161
[9f1362d4]162static void set_style(uint8_t style)
[429acb9]163{
[424cd43]164 async_msg_1(fb_info.phone, FB_SET_STYLE, style);
[429acb9]165}
166
[9f1362d4]167static void set_color(uint8_t fgcolor, uint8_t bgcolor, uint8_t flags)
[429acb9]168{
[9805cde]169 async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
170}
171
[9f1362d4]172static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor)
[9805cde]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 }
[429acb9]191}
192
[96b02eb9]193static int ccap_fb_to_con(sysarg_t ccap_fb, sysarg_t *ccap_con)
[50cfa6c]194{
195 switch (ccap_fb) {
[9f1362d4]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;
[50cfa6c]210 }
[9f1362d4]211
[50cfa6c]212 return EOK;
213}
214
[dc033a1]215/** Send an area of screenbuffer to the FB driver. */
[96b02eb9]216static void fb_update_area(console_t *cons, sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height)
[d2cc7e1]217{
[dc033a1]218 if (interbuffer) {
[96b02eb9]219 sysarg_t x;
220 sysarg_t y;
[424cd43]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);
[dc033a1]226 }
227 }
[1601f3c]228
229 async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]230 x0, y0, width, height);
[dc033a1]231 }
[d2cc7e1]232}
233
[dc033a1]234/** Flush pending cells to FB. */
235static void fb_pending_flush(void)
[d2cc7e1]236{
[1601f3c]237 if (fb_pending.cnt > 0) {
[424cd43]238 fb_update_area(active_console, fb_pending.col,
[1601f3c]239 fb_pending.row, fb_pending.cnt, 1);
240 fb_pending.cnt = 0;
[d2cc7e1]241 }
242}
243
[dc033a1]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.
[1601f3c]248 *
[dc033a1]249 */
[96b02eb9]250static void cell_mark_changed(sysarg_t col, sysarg_t row)
[429acb9]251{
[1601f3c]252 if (fb_pending.cnt != 0) {
[424cd43]253 if ((col != fb_pending.col + fb_pending.cnt)
254 || (row != fb_pending.row)) {
[dc033a1]255 fb_pending_flush();
256 }
257 }
[1601f3c]258
259 if (fb_pending.cnt == 0) {
[dc033a1]260 fb_pending.col = col;
[424cd43]261 fb_pending.row = row;
[d2cc7e1]262 }
[1601f3c]263
264 fb_pending.cnt++;
[429acb9]265}
266
[dc033a1]267/** Print a character to the active VC with buffering. */
[96b02eb9]268static void fb_putchar(wchar_t c, sysarg_t col, sysarg_t row)
[dc033a1]269{
[424cd43]270 async_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
[dc033a1]271}
272
273/** Process a character from the client (TTY emulation). */
[424cd43]274static void write_char(console_t *cons, wchar_t ch)
[10569b1]275{
[0ed2e0e]276 bool flush_cursor = false;
[9f1362d4]277
[7ce3cb2]278 switch (ch) {
[00bb6965]279 case '\n':
[dc033a1]280 fb_pending_flush();
[0ed2e0e]281 flush_cursor = true;
[424cd43]282 cons->scr.position_y++;
283 cons->scr.position_x = 0;
[00bb6965]284 break;
285 case '\r':
286 break;
287 case '\t':
[424cd43]288 cons->scr.position_x += 8;
289 cons->scr.position_x -= cons->scr.position_x % 8;
[00bb6965]290 break;
291 case '\b':
[424cd43]292 if (cons->scr.position_x == 0)
[10569b1]293 break;
[424cd43]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, ' ');
[00bb6965]298 break;
[1601f3c]299 default:
[424cd43]300 if (cons == active_console)
301 cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
[1601f3c]302
[424cd43]303 screenbuffer_putchar(&cons->scr, ch);
304 cons->scr.position_x++;
[10569b1]305 }
[1601f3c]306
[0ed2e0e]307 if (cons->scr.position_x >= cons->scr.size_x) {
308 flush_cursor = true;
[424cd43]309 cons->scr.position_y++;
[0ed2e0e]310 }
[10569b1]311
[424cd43]312 if (cons->scr.position_y >= cons->scr.size_y) {
[dc033a1]313 fb_pending_flush();
[424cd43]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)
[0cc4313]319 async_msg_1(fb_info.phone, FB_SCROLL, 1);
[10569b1]320 }
[9f1362d4]321
[0ed2e0e]322 if (cons == active_console && flush_cursor)
323 curs_goto(cons->scr.position_x, cons->scr.position_y);
[424cd43]324 cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
[10569b1]325}
326
[429acb9]327/** Switch to new console */
[424cd43]328static void change_console(console_t *cons)
[429acb9]329{
[700af62]330 if (cons == active_console) {
[429acb9]331 return;
[700af62]332 }
[1601f3c]333
[dc033a1]334 fb_pending_flush();
[1601f3c]335
[424cd43]336 if (cons == kernel_console) {
[1f83244]337 async_serialize_start();
[8c6337d]338 curs_hide_sync();
[76fca31]339 gcons_in_kernel();
[ebfabf6]340 screen_yield();
[ccd1a14]341 kbd_yield();
[1f83244]342 async_serialize_end();
[1601f3c]343
[3ad953c]344 if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
345 prev_console = active_console;
[424cd43]346 active_console = kernel_console;
[3ad953c]347 } else
[424cd43]348 cons = active_console;
[01f5e17]349 }
[6d5005c]350
[424cd43]351 if (cons != kernel_console) {
[76fca31]352 async_serialize_start();
353
[424cd43]354 if (active_console == kernel_console) {
[ebfabf6]355 screen_reclaim();
[ccd1a14]356 kbd_reclaim();
[76fca31]357 gcons_redraw_console();
[10270a8]358 }
[76fca31]359
[424cd43]360 active_console = cons;
361 gcons_change_console(cons->index);
[76fca31]362
[424cd43]363 set_attrs(&cons->scr.attrs);
[8c6337d]364 curs_visibility(false);
[9f1362d4]365
[96b02eb9]366 sysarg_t x;
367 sysarg_t y;
[9f1362d4]368 int rc = 0;
369
[76fca31]370 if (interbuffer) {
[424cd43]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);
[76fca31]375 }
[dc033a1]376 }
[424cd43]377
[76fca31]378 /* This call can preempt, but we are already at the end */
[dc033a1]379 rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
[424cd43]380 0, 0, cons->scr.size_x,
381 cons->scr.size_y);
[76fca31]382 }
383
384 if ((!interbuffer) || (rc != 0)) {
[424cd43]385 set_attrs(&cons->scr.attrs);
386 screen_clear();
[76fca31]387
[424cd43]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))
[9805cde]393 set_attrs(&field->attrs);
[424cd43]394
395 cons->scr.attrs = field->attrs;
[76fca31]396 if ((field->character == ' ') &&
[424cd43]397 (attrs_same(field->attrs, cons->scr.attrs)))
[76fca31]398 continue;
[1601f3c]399
[424cd43]400 fb_putchar(field->character, x, y);
[76fca31]401 }
402 }
403
[424cd43]404 curs_goto(cons->scr.position_x, cons->scr.position_y);
405 curs_visibility(cons->scr.is_cursor_visible);
[76fca31]406
407 async_serialize_end();
[429acb9]408 }
409}
[10569b1]410
[a2a3763]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
[e87e18f]424/** Handler for keyboard */
[eaf34f7]425static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
[51c1b003]426{
[424cd43]427 /* Ignore parameters, the connection is already opened */
[1601f3c]428 while (true) {
[424cd43]429 ipc_call_t call;
430 ipc_callid_t callid = async_get_call(&call);
431
432 int retval;
433 console_event_t ev;
434
[228e490]435 switch (IPC_GET_IMETHOD(call)) {
[eaf34f7]436 case IPC_M_PHONE_HUNGUP:
437 /* TODO: Handle hangup */
[a2a3763]438 close_driver_phone(iid);
[eaf34f7]439 return;
[fa09449]440 case KBD_EVENT:
441 /* Got event from keyboard driver. */
[eaf34f7]442 retval = 0;
[fa09449]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
[f89979b]448 if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
[0175246]449 CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
[424cd43]450 if (ev.key == KC_F1 + KERNEL_CONSOLE)
451 change_console(kernel_console);
[429acb9]452 else
[424cd43]453 change_console(&consoles[ev.key - KC_F1]);
[eaf34f7]454 break;
455 }
[cf28036c]456
[953769f]457 fibril_mutex_lock(&input_mutex);
[424cd43]458 keybuffer_push(&active_console->keybuffer, &ev);
[af65b72]459 fibril_condvar_broadcast(&input_cv);
[953769f]460 fibril_mutex_unlock(&input_mutex);
[eaf34f7]461 break;
462 default:
[1029d3d3]463 retval = ENOENT;
[085bd54]464 }
[ffa2c8ef]465 async_answer_0(callid, retval);
[eaf34f7]466 }
467}
468
[9f51afc]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);
[9f1362d4]476
[9f51afc]477 int retval;
[9f1362d4]478
[228e490]479 switch (IPC_GET_IMETHOD(call)) {
[9f51afc]480 case IPC_M_PHONE_HUNGUP:
481 /* TODO: Handle hangup */
[a2a3763]482 close_driver_phone(iid);
[9f51afc]483 return;
484 case MEVENT_BUTTON:
[9f1362d4]485 if (IPC_GET_ARG1(call) == 1) {
486 int newcon = gcons_mouse_btn((bool) IPC_GET_ARG2(call));
[700af62]487 if (newcon != -1) {
[9f51afc]488 change_console(&consoles[newcon]);
[700af62]489 }
[9f51afc]490 }
491 retval = 0;
492 break;
493 case MEVENT_MOVE:
[9f1362d4]494 gcons_mouse_move((int) IPC_GET_ARG1(call),
495 (int) IPC_GET_ARG2(call));
[9f51afc]496 retval = 0;
497 break;
498 default:
499 retval = ENOENT;
500 }
501
[ffa2c8ef]502 async_answer_0(callid, retval);
[9f51afc]503 }
504}
505
[424cd43]506static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
[d2cc7e1]507{
[472c09d]508 void *buf;
[171f9a1]509 size_t size;
[eda925a]510 int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
[1601f3c]511
[472c09d]512 if (rc != EOK) {
[ffa2c8ef]513 async_answer_0(rid, rc);
[424cd43]514 return;
515 }
[1601f3c]516
[6568225]517 async_serialize_start();
[1601f3c]518
[424cd43]519 size_t off = 0;
[171f9a1]520 while (off < size) {
[424cd43]521 wchar_t ch = str_decode(buf, &off, size);
522 write_char(cons, ch);
[d2cc7e1]523 }
[1601f3c]524
[6568225]525 async_serialize_end();
[1601f3c]526
[424cd43]527 gcons_notify_char(cons->index);
[ffa2c8ef]528 async_answer_1(rid, EOK, size);
[424cd43]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;
[0da4e41]537 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]538 async_answer_0(callid, EINVAL);
539 async_answer_0(rid, EINVAL);
[424cd43]540 return;
541 }
542
543 char *buf = (char *) malloc(size);
544 if (buf == NULL) {
[ffa2c8ef]545 async_answer_0(callid, ENOMEM);
546 async_answer_0(rid, ENOMEM);
[424cd43]547 return;
548 }
549
550 size_t pos = 0;
551 console_event_t ev;
[953769f]552 fibril_mutex_lock(&input_mutex);
[9f1362d4]553
[af65b72]554recheck:
[424cd43]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) {
[0da4e41]563 (void) async_data_read_finalize(callid, buf, size);
[ffa2c8ef]564 async_answer_1(rid, EOK, size);
[424cd43]565 free(buf);
566 } else {
[af65b72]567 fibril_condvar_wait(&input_cv, &input_mutex);
568 goto recheck;
[424cd43]569 }
[9f1362d4]570
[953769f]571 fibril_mutex_unlock(&input_mutex);
[424cd43]572}
573
574static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
575{
576 console_event_t ev;
[9f1362d4]577
[953769f]578 fibril_mutex_lock(&input_mutex);
[9f1362d4]579
[af65b72]580recheck:
[424cd43]581 if (keybuffer_pop(&cons->keybuffer, &ev)) {
[ffa2c8ef]582 async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
[424cd43]583 } else {
[af65b72]584 fibril_condvar_wait(&input_cv, &input_mutex);
585 goto recheck;
[424cd43]586 }
[9f1362d4]587
[953769f]588 fibril_mutex_unlock(&input_mutex);
[d2cc7e1]589}
590
[eaf34f7]591/** Default thread for new connections */
[b1f51f0]592static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
[eaf34f7]593{
[424cd43]594 console_t *cons = NULL;
[3ad953c]595
[424cd43]596 size_t i;
597 for (i = 0; i < CONSOLE_COUNT; i++) {
598 if (i == KERNEL_CONSOLE)
599 continue;
600
[991f645]601 if (consoles[i].devmap_handle == (devmap_handle_t) IPC_GET_ARG1(*icall)) {
[424cd43]602 cons = &consoles[i];
603 break;
604 }
605 }
606
607 if (cons == NULL) {
[ffa2c8ef]608 async_answer_0(iid, ENOENT);
[eaf34f7]609 return;
610 }
[424cd43]611
612 ipc_callid_t callid;
613 ipc_call_t call;
[96b02eb9]614 sysarg_t arg1;
615 sysarg_t arg2;
616 sysarg_t arg3;
[9f1362d4]617
[50cfa6c]618 int rc;
[a7d2d78]619
[085bd54]620 async_serialize_start();
[424cd43]621 if (cons->refcount == 0)
622 gcons_notify_connect(cons->index);
623
624 cons->refcount++;
[10569b1]625
[eaf34f7]626 /* Accept the connection */
[ffa2c8ef]627 async_answer_0(iid, EOK);
[3ad953c]628
[1601f3c]629 while (true) {
[085bd54]630 async_serialize_end();
[eaf34f7]631 callid = async_get_call(&call);
[085bd54]632 async_serialize_start();
[3ad953c]633
[96858e8]634 arg1 = 0;
635 arg2 = 0;
[fa09449]636 arg3 = 0;
[1601f3c]637
[228e490]638 switch (IPC_GET_IMETHOD(call)) {
[eaf34f7]639 case IPC_M_PHONE_HUNGUP:
[424cd43]640 cons->refcount--;
641 if (cons->refcount == 0)
642 gcons_notify_disconnect(cons->index);
[eaf34f7]643 return;
[4198f9c3]644 case VFS_OUT_READ:
[424cd43]645 async_serialize_end();
646 cons_read(cons, callid, &call);
647 async_serialize_start();
648 continue;
[4198f9c3]649 case VFS_OUT_WRITE:
[6568225]650 async_serialize_end();
[424cd43]651 cons_write(cons, callid, &call);
[6568225]652 async_serialize_start();
[d2cc7e1]653 continue;
[4198f9c3]654 case VFS_OUT_SYNC:
[424cd43]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;
[ad123964]661 case CONSOLE_CLEAR:
[3993b3d]662 /* Send message to fb */
[424cd43]663 if (cons == active_console)
664 async_msg_0(fb_info.phone, FB_CLEAR);
[3993b3d]665
[424cd43]666 screenbuffer_clear(&cons->scr);
[3993b3d]667
[eaf34f7]668 break;
[ad123964]669 case CONSOLE_GOTO:
[424cd43]670 screenbuffer_goto(&cons->scr,
671 IPC_GET_ARG1(call), IPC_GET_ARG2(call));
672 if (cons == active_console)
[00bb6965]673 curs_goto(IPC_GET_ARG1(call),
[01f5e17]674 IPC_GET_ARG2(call));
[ad123964]675 break;
[19528516]676 case CONSOLE_GET_POS:
677 arg1 = cons->scr.position_x;
678 arg2 = cons->scr.position_y;
679 break;
[424cd43]680 case CONSOLE_GET_SIZE:
681 arg1 = fb_info.cols;
682 arg2 = fb_info.rows;
[a9bd960c]683 break;
[50cfa6c]684 case CONSOLE_GET_COLOR_CAP:
[9f1362d4]685 rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
[50cfa6c]686 if (rc != EOK) {
[ffa2c8ef]687 async_answer_0(callid, rc);
[50cfa6c]688 continue;
689 }
690 break;
[a9bd960c]691 case CONSOLE_SET_STYLE:
[dc033a1]692 fb_pending_flush();
[9805cde]693 arg1 = IPC_GET_ARG1(call);
[424cd43]694 screenbuffer_set_style(&cons->scr, arg1);
695 if (cons == active_console)
[9805cde]696 set_style(arg1);
697 break;
698 case CONSOLE_SET_COLOR:
[dc033a1]699 fb_pending_flush();
[9805cde]700 arg1 = IPC_GET_ARG1(call);
701 arg2 = IPC_GET_ARG2(call);
702 arg3 = IPC_GET_ARG3(call);
[424cd43]703 screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
704 if (cons == active_console)
[9805cde]705 set_color(arg1, arg2, arg3);
706 break;
707 case CONSOLE_SET_RGB_COLOR:
[dc033a1]708 fb_pending_flush();
[a9bd960c]709 arg1 = IPC_GET_ARG1(call);
710 arg2 = IPC_GET_ARG2(call);
[424cd43]711 screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
712 if (cons == active_console)
[9805cde]713 set_rgb_color(arg1, arg2);
[0c6984e]714 break;
[a8b2b5b2]715 case CONSOLE_CURSOR_VISIBILITY:
[dc033a1]716 fb_pending_flush();
[a8b2b5b2]717 arg1 = IPC_GET_ARG1(call);
[424cd43]718 cons->scr.is_cursor_visible = arg1;
719 if (cons == active_console)
[a8b2b5b2]720 curs_visibility(arg1);
721 break;
[424cd43]722 case CONSOLE_GET_EVENT:
723 async_serialize_end();
724 cons_get_event(cons, callid, &call);
725 async_serialize_start();
726 continue;
[3fe00ee]727 case CONSOLE_KCON_ENABLE:
[424cd43]728 change_console(kernel_console);
[3fe00ee]729 break;
[eaf34f7]730 }
[ffa2c8ef]731 async_answer_3(callid, EOK, arg1, arg2, arg3);
[eaf34f7]732 }
733}
734
[3ad953c]735static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
736{
737 change_console(prev_console);
738}
739
[a2a3763]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
[103ae7f8]761static int connect_keyboard_or_mouse(const char *devname,
762 async_client_conn_t handler, const char *path)
[eaf34f7]763{
[700af62]764 int fd = open(path, O_RDONLY);
765 if (fd < 0) {
766 return fd;
[47a350f]767 }
[9f1362d4]768
[700af62]769 int phone = fd_phone(fd);
770 if (phone < 0) {
[47a350f]771 printf(NAME ": Failed to connect to input device\n");
[700af62]772 return phone;
[00bb6965]773 }
[9f1362d4]774
[a2a3763]775 ipc_callid_t hash;
776 int rc = async_connect_to_me_hack(phone, SERVICE_CONSOLE, 0, phone,
777 handler, &hash);
[700af62]778 if (rc != EOK) {
[30db06c]779 printf(NAME ": " \
780 "Failed to create callback from input device: %s.\n",
781 str_error(rc));
[700af62]782 return rc;
[4904de8]783 }
[9f1362d4]784
[a2a3763]785 driver_phones[phone] = hash;
786
787 printf(NAME ": found %s \"%s\" (%" PRIxn ").\n", devname, path, hash);
[700af62]788
789 return phone;
790}
791
[103ae7f8]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};
[30db06c]806
807/** Periodically check for new keyboards in /dev/class/.
[f03d3786]808 *
[30db06c]809 * @param arg Class name.
810 * @return This function should never exit.
[f03d3786]811 */
[103ae7f8]812static int check_new_device_fibril(void *arg)
[700af62]813{
[103ae7f8]814 struct hid_class_info *dev_info = arg;
[700af62]815
[30db06c]816 size_t index = 1;
[700af62]817
818 while (true) {
[30db06c]819 async_usleep(HOTPLUG_WATCH_INTERVAL);
[700af62]820 char *path;
[30db06c]821 int rc = asprintf(&path, "/dev/class/%s\\%zu",
[103ae7f8]822 dev_info->classname, index);
[700af62]823 if (rc < 0) {
824 continue;
825 }
826 rc = 0;
[103ae7f8]827 rc = dev_info->connection_func(path);
[700af62]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 */
[103ae7f8]842static void check_new_devices_in_background(int (*connection_func)(const char *),
843 const char *classname)
[700af62]844{
[103ae7f8]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);
[700af62]862 if (!fid) {
[103ae7f8]863 printf(NAME
864 ": failed to create hot-plug-watch fibril for %s.\n",
865 classname);
[700af62]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
[103ae7f8]879 mouse_phone = connect_mouse("/dev/hid_in/mouse");
[9f51afc]880 if (mouse_phone < 0) {
[103ae7f8]881 printf(NAME ": Failed to connect to mouse device: %s.\n",
882 str_error(mouse_phone));
[9f51afc]883 }
[9f1362d4]884
[51c1b003]885 /* Connect to framebuffer driver */
[007e6efa]886 fb_info.phone = service_connect_blocking(SERVICE_VIDEO, 0, 0);
[4904de8]887 if (fb_info.phone < 0) {
888 printf(NAME ": Failed to connect to video service\n");
889 return -1;
[3993b3d]890 }
[9f1362d4]891
[424cd43]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 }
[516ff92]898
[e1c4849]899 /* Initialize gcons */
900 gcons_init(fb_info.phone);
[3993b3d]901
[424cd43]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);
[9f1362d4]905 async_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
[1601f3c]906
[7447572]907 /* Set up shared memory buffer. */
[424cd43]908 size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
[7447572]909 interbuffer = as_get_mappable_page(ib_size);
[1601f3c]910
[7447572]911 if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
[424cd43]912 AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
[7447572]913 interbuffer = NULL;
[1601f3c]914
[7447572]915 if (interbuffer) {
[0da4e41]916 if (async_share_out_start(fb_info.phone, interbuffer,
[27d293a]917 AS_AREA_READ) != EOK) {
[7447572]918 as_area_destroy(interbuffer);
[390a678]919 interbuffer = NULL;
920 }
921 }
[3ad953c]922
[424cd43]923 fb_pending.cnt = 0;
[3ad953c]924
[424cd43]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) {
[7e752b2]931 printf(NAME ": Unable to allocate screen buffer %zu\n", i);
[424cd43]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
[1313ee9]939 char vc[DEVMAP_NAME_MAXLEN + 1];
[7e752b2]940 snprintf(vc, DEVMAP_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
[424cd43]941
[991f645]942 if (devmap_device_register(vc, &consoles[i].devmap_handle) != EOK) {
[424cd43]943 printf(NAME ": Unable to register device %s\n", vc);
944 return false;
945 }
946 }
947 }
948
[1035437]949 /* Disable kernel output to the console */
950 __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
951
[424cd43]952 /* Initialize the screen */
[953769f]953 async_serialize_start();
[1035437]954 gcons_redraw_console();
[369a5f8]955 set_style(STYLE_NORMAL);
[424cd43]956 screen_clear();
957 curs_goto(0, 0);
958 curs_visibility(active_console->scr.is_cursor_visible);
[953769f]959 async_serialize_end();
[51c1b003]960
[3ad953c]961 /* Receive kernel notifications */
[007e6efa]962 async_set_interrupt_received(interrupt_received);
[05641a9e]963 if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
964 printf(NAME ": Error registering kconsole notifications\n");
[1601f3c]965
[700af62]966 /* Start fibril for checking on hot-plugged keyboards. */
[103ae7f8]967 check_new_devices_in_background(connect_keyboard, "keyboard");
968 check_new_devices_in_background(connect_mouse, "mouse");
[700af62]969
[424cd43]970 return true;
971}
972
[47a350f]973static void usage(void)
974{
975 printf("Usage: console <input>\n");
976}
977
[424cd43]978int main(int argc, char *argv[])
979{
[47a350f]980 if (argc < 2) {
981 usage();
982 return -1;
983 }
984
[424cd43]985 printf(NAME ": HelenOS Console service\n");
986
[47a350f]987 if (!console_init(argv[1]))
[424cd43]988 return -1;
[700af62]989
[424cd43]990 printf(NAME ": Accepting connections\n");
[eaf34f7]991 async_manager();
[3ad953c]992
993 return 0;
[51c1b003]994}
[516ff92]995
[ce5bcb4]996/** @}
997 */
Note: See TracBrowser for help on using the repository browser.