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