1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup console
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <libc.h>
|
---|
37 | #include <ipc/input.h>
|
---|
38 | #include <io/keycode.h>
|
---|
39 | #include <ipc/fb.h>
|
---|
40 | #include <ipc/services.h>
|
---|
41 | #include <ns.h>
|
---|
42 | #include <ns_obsolete.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <ipc/console.h>
|
---|
46 | #include <unistd.h>
|
---|
47 | #include <async.h>
|
---|
48 | #include <async_obsolete.h>
|
---|
49 | #include <adt/fifo.h>
|
---|
50 | #include <sys/mman.h>
|
---|
51 | #include <stdio.h>
|
---|
52 | #include <str.h>
|
---|
53 | #include <sysinfo.h>
|
---|
54 | #include <event.h>
|
---|
55 | #include <loc.h>
|
---|
56 | #include <fcntl.h>
|
---|
57 | #include <vfs/vfs.h>
|
---|
58 | #include <fibril_synch.h>
|
---|
59 | #include <io/style.h>
|
---|
60 | #include <io/screenbuffer.h>
|
---|
61 |
|
---|
62 | #include "console.h"
|
---|
63 | #include "gcons.h"
|
---|
64 | #include "keybuffer.h"
|
---|
65 |
|
---|
66 | #define NAME "console"
|
---|
67 | #define NAMESPACE "term"
|
---|
68 |
|
---|
69 | /** Session with the input server. */
|
---|
70 | static async_sess_t *input_sess;
|
---|
71 |
|
---|
72 | /** Information about framebuffer */
|
---|
73 | struct {
|
---|
74 | int phone; /**< Framebuffer phone */
|
---|
75 | sysarg_t cols; /**< Framebuffer columns */
|
---|
76 | sysarg_t rows; /**< Framebuffer rows */
|
---|
77 | sysarg_t color_cap; /**< Color capabilities (FB_CCAP_xxx) */
|
---|
78 | } fb_info;
|
---|
79 |
|
---|
80 | typedef struct {
|
---|
81 | size_t index; /**< Console index */
|
---|
82 | size_t refcount; /**< Connection reference count */
|
---|
83 | service_id_t service_id; /**< Service ID */
|
---|
84 | keybuffer_t keybuffer; /**< Buffer for incoming keys. */
|
---|
85 | screenbuffer_t scr; /**< Screenbuffer for saving screen
|
---|
86 | contents and related settings. */
|
---|
87 | } console_t;
|
---|
88 |
|
---|
89 | /** Array of data for virtual consoles */
|
---|
90 | static console_t consoles[CONSOLE_COUNT];
|
---|
91 |
|
---|
92 | static console_t *active_console = &consoles[0];
|
---|
93 | static console_t *prev_console = &consoles[0];
|
---|
94 | static console_t *kernel_console = &consoles[KERNEL_CONSOLE];
|
---|
95 |
|
---|
96 | /** Pointer to memory shared with framebufer used for
|
---|
97 | faster virtual console switching */
|
---|
98 | static keyfield_t *interbuffer = NULL;
|
---|
99 |
|
---|
100 | /** Information on row-span yet unsent to FB driver. */
|
---|
101 | struct {
|
---|
102 | sysarg_t col; /**< Leftmost column of the span. */
|
---|
103 | sysarg_t row; /**< Row where the span lies. */
|
---|
104 | sysarg_t cnt; /**< Width of the span. */
|
---|
105 | } fb_pending;
|
---|
106 |
|
---|
107 | static FIBRIL_MUTEX_INITIALIZE(input_mutex);
|
---|
108 | static FIBRIL_CONDVAR_INITIALIZE(input_cv);
|
---|
109 |
|
---|
110 | static FIBRIL_MUTEX_INITIALIZE(big_console_lock);
|
---|
111 |
|
---|
112 | static void console_serialize_start(void)
|
---|
113 | {
|
---|
114 | fibril_mutex_lock(&big_console_lock);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void console_serialize_end(void)
|
---|
118 | {
|
---|
119 | fibril_mutex_unlock(&big_console_lock);
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void curs_visibility(bool visible)
|
---|
123 | {
|
---|
124 | async_obsolete_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
|
---|
125 | }
|
---|
126 |
|
---|
127 | static void curs_hide_sync(void)
|
---|
128 | {
|
---|
129 | async_obsolete_req_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static void curs_goto(sysarg_t x, sysarg_t y)
|
---|
133 | {
|
---|
134 | async_obsolete_msg_2(fb_info.phone, FB_CURSOR_GOTO, x, y);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void screen_clear(void)
|
---|
138 | {
|
---|
139 | async_obsolete_msg_0(fb_info.phone, FB_CLEAR);
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void screen_yield(void)
|
---|
143 | {
|
---|
144 | async_obsolete_req_0_0(fb_info.phone, FB_SCREEN_YIELD);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static void screen_reclaim(void)
|
---|
148 | {
|
---|
149 | async_obsolete_req_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void input_yield(void)
|
---|
153 | {
|
---|
154 | async_exch_t *exch = async_exchange_begin(input_sess);
|
---|
155 | if (exch == NULL) {
|
---|
156 | printf("%s: Failed starting exchange with input device.\n",
|
---|
157 | NAME);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | async_req_0_0(exch, INPUT_YIELD);
|
---|
162 | async_exchange_end(exch);
|
---|
163 | }
|
---|
164 |
|
---|
165 | static void input_reclaim(void)
|
---|
166 | {
|
---|
167 | async_exch_t *exch = async_exchange_begin(input_sess);
|
---|
168 | if (exch == NULL) {
|
---|
169 | printf("%s: Failed starting exchange with input device.\n",
|
---|
170 | NAME);
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | async_req_0_0(exch, INPUT_RECLAIM);
|
---|
175 | async_exchange_end(exch);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static void set_style(uint8_t style)
|
---|
179 | {
|
---|
180 | async_obsolete_msg_1(fb_info.phone, FB_SET_STYLE, style);
|
---|
181 | }
|
---|
182 |
|
---|
183 | static void set_color(uint8_t fgcolor, uint8_t bgcolor, uint8_t flags)
|
---|
184 | {
|
---|
185 | async_obsolete_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
|
---|
186 | }
|
---|
187 |
|
---|
188 | static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor)
|
---|
189 | {
|
---|
190 | async_obsolete_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void set_attrs(attrs_t *attrs)
|
---|
194 | {
|
---|
195 | switch (attrs->t) {
|
---|
196 | case at_style:
|
---|
197 | set_style(attrs->a.s.style);
|
---|
198 | break;
|
---|
199 | case at_idx:
|
---|
200 | set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
|
---|
201 | attrs->a.i.flags);
|
---|
202 | break;
|
---|
203 | case at_rgb:
|
---|
204 | set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | static int ccap_fb_to_con(sysarg_t ccap_fb, sysarg_t *ccap_con)
|
---|
210 | {
|
---|
211 | switch (ccap_fb) {
|
---|
212 | case FB_CCAP_NONE:
|
---|
213 | *ccap_con = CONSOLE_CCAP_NONE;
|
---|
214 | break;
|
---|
215 | case FB_CCAP_STYLE:
|
---|
216 | *ccap_con = CONSOLE_CCAP_STYLE;
|
---|
217 | break;
|
---|
218 | case FB_CCAP_INDEXED:
|
---|
219 | *ccap_con = CONSOLE_CCAP_INDEXED;
|
---|
220 | break;
|
---|
221 | case FB_CCAP_RGB:
|
---|
222 | *ccap_con = CONSOLE_CCAP_RGB;
|
---|
223 | break;
|
---|
224 | default:
|
---|
225 | return EINVAL;
|
---|
226 | }
|
---|
227 |
|
---|
228 | return EOK;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /** Send an area of screenbuffer to the FB driver. */
|
---|
232 | static void fb_update_area(console_t *cons, sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height)
|
---|
233 | {
|
---|
234 | if (interbuffer) {
|
---|
235 | sysarg_t x;
|
---|
236 | sysarg_t y;
|
---|
237 |
|
---|
238 | for (y = 0; y < height; y++) {
|
---|
239 | for (x = 0; x < width; x++) {
|
---|
240 | interbuffer[y * width + x] =
|
---|
241 | *get_field_at(&cons->scr, x0 + x, y0 + y);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | async_obsolete_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
|
---|
246 | x0, y0, width, height);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /** Flush pending cells to FB. */
|
---|
251 | static void fb_pending_flush(void)
|
---|
252 | {
|
---|
253 | if (fb_pending.cnt > 0) {
|
---|
254 | fb_update_area(active_console, fb_pending.col,
|
---|
255 | fb_pending.row, fb_pending.cnt, 1);
|
---|
256 | fb_pending.cnt = 0;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | /** Mark a character cell as changed.
|
---|
261 | *
|
---|
262 | * This adds the cell to the pending rowspan if possible. Otherwise
|
---|
263 | * the old span is flushed first.
|
---|
264 | *
|
---|
265 | */
|
---|
266 | static void cell_mark_changed(sysarg_t col, sysarg_t row)
|
---|
267 | {
|
---|
268 | if (fb_pending.cnt != 0) {
|
---|
269 | if ((col != fb_pending.col + fb_pending.cnt)
|
---|
270 | || (row != fb_pending.row)) {
|
---|
271 | fb_pending_flush();
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (fb_pending.cnt == 0) {
|
---|
276 | fb_pending.col = col;
|
---|
277 | fb_pending.row = row;
|
---|
278 | }
|
---|
279 |
|
---|
280 | fb_pending.cnt++;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** Print a character to the active VC with buffering. */
|
---|
284 | static void fb_putchar(wchar_t c, sysarg_t col, sysarg_t row)
|
---|
285 | {
|
---|
286 | async_obsolete_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Process a character from the client (TTY emulation). */
|
---|
290 | static void write_char(console_t *cons, wchar_t ch)
|
---|
291 | {
|
---|
292 | bool flush_cursor = false;
|
---|
293 |
|
---|
294 | switch (ch) {
|
---|
295 | case '\n':
|
---|
296 | fb_pending_flush();
|
---|
297 | flush_cursor = true;
|
---|
298 | cons->scr.position_y++;
|
---|
299 | cons->scr.position_x = 0;
|
---|
300 | break;
|
---|
301 | case '\r':
|
---|
302 | break;
|
---|
303 | case '\t':
|
---|
304 | cons->scr.position_x += 8;
|
---|
305 | cons->scr.position_x -= cons->scr.position_x % 8;
|
---|
306 | break;
|
---|
307 | case '\b':
|
---|
308 | if (cons->scr.position_x == 0)
|
---|
309 | break;
|
---|
310 | cons->scr.position_x--;
|
---|
311 | if (cons == active_console)
|
---|
312 | cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
|
---|
313 | screenbuffer_putchar(&cons->scr, ' ');
|
---|
314 | break;
|
---|
315 | default:
|
---|
316 | if (cons == active_console)
|
---|
317 | cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
|
---|
318 |
|
---|
319 | screenbuffer_putchar(&cons->scr, ch);
|
---|
320 | cons->scr.position_x++;
|
---|
321 | }
|
---|
322 |
|
---|
323 | if (cons->scr.position_x >= cons->scr.size_x) {
|
---|
324 | flush_cursor = true;
|
---|
325 | cons->scr.position_y++;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (cons->scr.position_y >= cons->scr.size_y) {
|
---|
329 | fb_pending_flush();
|
---|
330 | cons->scr.position_y = cons->scr.size_y - 1;
|
---|
331 | screenbuffer_clear_line(&cons->scr, cons->scr.top_line);
|
---|
332 | cons->scr.top_line = (cons->scr.top_line + 1) % cons->scr.size_y;
|
---|
333 |
|
---|
334 | if (cons == active_console)
|
---|
335 | async_obsolete_msg_1(fb_info.phone, FB_SCROLL, 1);
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (cons == active_console && flush_cursor)
|
---|
339 | curs_goto(cons->scr.position_x, cons->scr.position_y);
|
---|
340 | cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Switch to new console */
|
---|
344 | static void change_console(console_t *cons)
|
---|
345 | {
|
---|
346 | if (cons == active_console)
|
---|
347 | return;
|
---|
348 |
|
---|
349 | fb_pending_flush();
|
---|
350 |
|
---|
351 | if (cons == kernel_console) {
|
---|
352 | console_serialize_start();
|
---|
353 | curs_hide_sync();
|
---|
354 | gcons_in_kernel();
|
---|
355 | screen_yield();
|
---|
356 | input_yield();
|
---|
357 | console_serialize_end();
|
---|
358 |
|
---|
359 | if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
|
---|
360 | prev_console = active_console;
|
---|
361 | active_console = kernel_console;
|
---|
362 | } else
|
---|
363 | cons = active_console;
|
---|
364 | }
|
---|
365 |
|
---|
366 | if (cons != kernel_console) {
|
---|
367 | console_serialize_start();
|
---|
368 |
|
---|
369 | if (active_console == kernel_console) {
|
---|
370 | screen_reclaim();
|
---|
371 | input_reclaim();
|
---|
372 | gcons_redraw_console();
|
---|
373 | }
|
---|
374 |
|
---|
375 | active_console = cons;
|
---|
376 | gcons_change_console(cons->index);
|
---|
377 |
|
---|
378 | set_attrs(&cons->scr.attrs);
|
---|
379 | curs_visibility(false);
|
---|
380 |
|
---|
381 | sysarg_t x;
|
---|
382 | sysarg_t y;
|
---|
383 | int rc = 0;
|
---|
384 |
|
---|
385 | if (interbuffer) {
|
---|
386 | for (y = 0; y < cons->scr.size_y; y++) {
|
---|
387 | for (x = 0; x < cons->scr.size_x; x++) {
|
---|
388 | interbuffer[y * cons->scr.size_x + x] =
|
---|
389 | *get_field_at(&cons->scr, x, y);
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* This call can preempt, but we are already at the end */
|
---|
394 | rc = async_obsolete_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
|
---|
395 | 0, 0, cons->scr.size_x,
|
---|
396 | cons->scr.size_y);
|
---|
397 | }
|
---|
398 |
|
---|
399 | if ((!interbuffer) || (rc != 0)) {
|
---|
400 | set_attrs(&cons->scr.attrs);
|
---|
401 | screen_clear();
|
---|
402 |
|
---|
403 | for (y = 0; y < cons->scr.size_y; y++)
|
---|
404 | for (x = 0; x < cons->scr.size_x; x++) {
|
---|
405 | keyfield_t *field = get_field_at(&cons->scr, x, y);
|
---|
406 |
|
---|
407 | if (!attrs_same(cons->scr.attrs, field->attrs))
|
---|
408 | set_attrs(&field->attrs);
|
---|
409 |
|
---|
410 | cons->scr.attrs = field->attrs;
|
---|
411 | if ((field->character == ' ') &&
|
---|
412 | (attrs_same(field->attrs, cons->scr.attrs)))
|
---|
413 | continue;
|
---|
414 |
|
---|
415 | fb_putchar(field->character, x, y);
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | curs_goto(cons->scr.position_x, cons->scr.position_y);
|
---|
420 | curs_visibility(cons->scr.is_cursor_visible);
|
---|
421 |
|
---|
422 | console_serialize_end();
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | /** Handler for input events */
|
---|
427 | static void input_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
428 | {
|
---|
429 | /* Ignore parameters, the connection is already opened */
|
---|
430 | while (true) {
|
---|
431 | ipc_call_t call;
|
---|
432 | ipc_callid_t callid = async_get_call(&call);
|
---|
433 |
|
---|
434 | int retval;
|
---|
435 | kbd_event_t ev;
|
---|
436 |
|
---|
437 | if (!IPC_GET_IMETHOD(call)) {
|
---|
438 | /* TODO: Handle hangup */
|
---|
439 | async_hangup(input_sess);
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | switch (IPC_GET_IMETHOD(call)) {
|
---|
444 | case INPUT_EVENT_KEY:
|
---|
445 | /* Got key press/release event */
|
---|
446 | retval = 0;
|
---|
447 | ev.type = IPC_GET_ARG1(call);
|
---|
448 | ev.key = IPC_GET_ARG2(call);
|
---|
449 | ev.mods = IPC_GET_ARG3(call);
|
---|
450 | ev.c = IPC_GET_ARG4(call);
|
---|
451 |
|
---|
452 | if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
|
---|
453 | CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
|
---|
454 | if (ev.key == KC_F1 + KERNEL_CONSOLE)
|
---|
455 | change_console(kernel_console);
|
---|
456 | else
|
---|
457 | change_console(&consoles[ev.key - KC_F1]);
|
---|
458 | break;
|
---|
459 | }
|
---|
460 |
|
---|
461 | fibril_mutex_lock(&input_mutex);
|
---|
462 | keybuffer_push(&active_console->keybuffer, &ev);
|
---|
463 | fibril_condvar_broadcast(&input_cv);
|
---|
464 | fibril_mutex_unlock(&input_mutex);
|
---|
465 | break;
|
---|
466 | case INPUT_EVENT_MOVE:
|
---|
467 | /* Got pointer move event */
|
---|
468 | gcons_mouse_move((int) IPC_GET_ARG1(call),
|
---|
469 | (int) IPC_GET_ARG2(call));
|
---|
470 | retval = 0;
|
---|
471 | break;
|
---|
472 | case INPUT_EVENT_BUTTON:
|
---|
473 | /* Got pointer button press/release event */
|
---|
474 | if (IPC_GET_ARG1(call) == 1) {
|
---|
475 | int newcon = gcons_mouse_btn((bool) IPC_GET_ARG2(call));
|
---|
476 | if (newcon != -1)
|
---|
477 | change_console(&consoles[newcon]);
|
---|
478 | }
|
---|
479 | retval = 0;
|
---|
480 | break;
|
---|
481 | default:
|
---|
482 | retval = ENOENT;
|
---|
483 | }
|
---|
484 |
|
---|
485 | async_answer_0(callid, retval);
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
|
---|
490 | {
|
---|
491 | void *buf;
|
---|
492 | size_t size;
|
---|
493 | int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
|
---|
494 |
|
---|
495 | if (rc != EOK) {
|
---|
496 | async_answer_0(rid, rc);
|
---|
497 | return;
|
---|
498 | }
|
---|
499 |
|
---|
500 | console_serialize_start();
|
---|
501 |
|
---|
502 | size_t off = 0;
|
---|
503 | while (off < size) {
|
---|
504 | wchar_t ch = str_decode(buf, &off, size);
|
---|
505 | write_char(cons, ch);
|
---|
506 | }
|
---|
507 |
|
---|
508 | console_serialize_end();
|
---|
509 |
|
---|
510 | gcons_notify_char(cons->index);
|
---|
511 | async_answer_1(rid, EOK, size);
|
---|
512 |
|
---|
513 | free(buf);
|
---|
514 | }
|
---|
515 |
|
---|
516 | static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
|
---|
517 | {
|
---|
518 | ipc_callid_t callid;
|
---|
519 | size_t size;
|
---|
520 | if (!async_data_read_receive(&callid, &size)) {
|
---|
521 | async_answer_0(callid, EINVAL);
|
---|
522 | async_answer_0(rid, EINVAL);
|
---|
523 | return;
|
---|
524 | }
|
---|
525 |
|
---|
526 | char *buf = (char *) malloc(size);
|
---|
527 | if (buf == NULL) {
|
---|
528 | async_answer_0(callid, ENOMEM);
|
---|
529 | async_answer_0(rid, ENOMEM);
|
---|
530 | return;
|
---|
531 | }
|
---|
532 |
|
---|
533 | size_t pos = 0;
|
---|
534 | kbd_event_t ev;
|
---|
535 | fibril_mutex_lock(&input_mutex);
|
---|
536 |
|
---|
537 | recheck:
|
---|
538 | while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) {
|
---|
539 | if (ev.type == KEY_PRESS) {
|
---|
540 | buf[pos] = ev.c;
|
---|
541 | pos++;
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | if (pos == size) {
|
---|
546 | (void) async_data_read_finalize(callid, buf, size);
|
---|
547 | async_answer_1(rid, EOK, size);
|
---|
548 | free(buf);
|
---|
549 | } else {
|
---|
550 | fibril_condvar_wait(&input_cv, &input_mutex);
|
---|
551 | goto recheck;
|
---|
552 | }
|
---|
553 |
|
---|
554 | fibril_mutex_unlock(&input_mutex);
|
---|
555 | }
|
---|
556 |
|
---|
557 | static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
|
---|
558 | {
|
---|
559 | kbd_event_t ev;
|
---|
560 |
|
---|
561 | fibril_mutex_lock(&input_mutex);
|
---|
562 |
|
---|
563 | recheck:
|
---|
564 | if (keybuffer_pop(&cons->keybuffer, &ev)) {
|
---|
565 | async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
|
---|
566 | } else {
|
---|
567 | fibril_condvar_wait(&input_cv, &input_mutex);
|
---|
568 | goto recheck;
|
---|
569 | }
|
---|
570 |
|
---|
571 | fibril_mutex_unlock(&input_mutex);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /** Default thread for new connections */
|
---|
575 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
576 | {
|
---|
577 | console_t *cons = NULL;
|
---|
578 |
|
---|
579 | size_t i;
|
---|
580 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
---|
581 | if (i == KERNEL_CONSOLE)
|
---|
582 | continue;
|
---|
583 |
|
---|
584 | if (consoles[i].service_id == (service_id_t) IPC_GET_ARG1(*icall)) {
|
---|
585 | cons = &consoles[i];
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | if (cons == NULL) {
|
---|
591 | async_answer_0(iid, ENOENT);
|
---|
592 | return;
|
---|
593 | }
|
---|
594 |
|
---|
595 | ipc_callid_t callid;
|
---|
596 | ipc_call_t call;
|
---|
597 | sysarg_t arg1;
|
---|
598 | sysarg_t arg2;
|
---|
599 | sysarg_t arg3;
|
---|
600 |
|
---|
601 | int rc;
|
---|
602 |
|
---|
603 | console_serialize_start();
|
---|
604 | if (cons->refcount == 0)
|
---|
605 | gcons_notify_connect(cons->index);
|
---|
606 |
|
---|
607 | cons->refcount++;
|
---|
608 |
|
---|
609 | /* Accept the connection */
|
---|
610 | async_answer_0(iid, EOK);
|
---|
611 |
|
---|
612 | while (true) {
|
---|
613 | console_serialize_end();
|
---|
614 | callid = async_get_call(&call);
|
---|
615 | console_serialize_start();
|
---|
616 |
|
---|
617 | arg1 = 0;
|
---|
618 | arg2 = 0;
|
---|
619 | arg3 = 0;
|
---|
620 |
|
---|
621 | if (!IPC_GET_IMETHOD(call)) {
|
---|
622 | cons->refcount--;
|
---|
623 | if (cons->refcount == 0)
|
---|
624 | gcons_notify_disconnect(cons->index);
|
---|
625 | console_serialize_end();
|
---|
626 | return;
|
---|
627 | }
|
---|
628 |
|
---|
629 | switch (IPC_GET_IMETHOD(call)) {
|
---|
630 | case VFS_OUT_READ:
|
---|
631 | console_serialize_end();
|
---|
632 | cons_read(cons, callid, &call);
|
---|
633 | console_serialize_start();
|
---|
634 | continue;
|
---|
635 | case VFS_OUT_WRITE:
|
---|
636 | console_serialize_end();
|
---|
637 | cons_write(cons, callid, &call);
|
---|
638 | console_serialize_start();
|
---|
639 | continue;
|
---|
640 | case VFS_OUT_SYNC:
|
---|
641 | fb_pending_flush();
|
---|
642 | if (cons == active_console) {
|
---|
643 | async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
|
---|
644 | curs_goto(cons->scr.position_x, cons->scr.position_y);
|
---|
645 | }
|
---|
646 | break;
|
---|
647 | case CONSOLE_CLEAR:
|
---|
648 | /* Send message to fb */
|
---|
649 | if (cons == active_console)
|
---|
650 | async_obsolete_msg_0(fb_info.phone, FB_CLEAR);
|
---|
651 |
|
---|
652 | screenbuffer_clear(&cons->scr);
|
---|
653 |
|
---|
654 | break;
|
---|
655 | case CONSOLE_GOTO:
|
---|
656 | screenbuffer_goto(&cons->scr,
|
---|
657 | IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
---|
658 | if (cons == active_console)
|
---|
659 | curs_goto(IPC_GET_ARG1(call),
|
---|
660 | IPC_GET_ARG2(call));
|
---|
661 | break;
|
---|
662 | case CONSOLE_GET_POS:
|
---|
663 | arg1 = cons->scr.position_x;
|
---|
664 | arg2 = cons->scr.position_y;
|
---|
665 | break;
|
---|
666 | case CONSOLE_GET_SIZE:
|
---|
667 | arg1 = fb_info.cols;
|
---|
668 | arg2 = fb_info.rows;
|
---|
669 | break;
|
---|
670 | case CONSOLE_GET_COLOR_CAP:
|
---|
671 | rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
|
---|
672 | if (rc != EOK) {
|
---|
673 | async_answer_0(callid, rc);
|
---|
674 | continue;
|
---|
675 | }
|
---|
676 | break;
|
---|
677 | case CONSOLE_SET_STYLE:
|
---|
678 | fb_pending_flush();
|
---|
679 | arg1 = IPC_GET_ARG1(call);
|
---|
680 | screenbuffer_set_style(&cons->scr, arg1);
|
---|
681 | if (cons == active_console)
|
---|
682 | set_style(arg1);
|
---|
683 | break;
|
---|
684 | case CONSOLE_SET_COLOR:
|
---|
685 | fb_pending_flush();
|
---|
686 | arg1 = IPC_GET_ARG1(call);
|
---|
687 | arg2 = IPC_GET_ARG2(call);
|
---|
688 | arg3 = IPC_GET_ARG3(call);
|
---|
689 | screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
|
---|
690 | if (cons == active_console)
|
---|
691 | set_color(arg1, arg2, arg3);
|
---|
692 | break;
|
---|
693 | case CONSOLE_SET_RGB_COLOR:
|
---|
694 | fb_pending_flush();
|
---|
695 | arg1 = IPC_GET_ARG1(call);
|
---|
696 | arg2 = IPC_GET_ARG2(call);
|
---|
697 | screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
|
---|
698 | if (cons == active_console)
|
---|
699 | set_rgb_color(arg1, arg2);
|
---|
700 | break;
|
---|
701 | case CONSOLE_CURSOR_VISIBILITY:
|
---|
702 | fb_pending_flush();
|
---|
703 | arg1 = IPC_GET_ARG1(call);
|
---|
704 | cons->scr.is_cursor_visible = arg1;
|
---|
705 | if (cons == active_console)
|
---|
706 | curs_visibility(arg1);
|
---|
707 | break;
|
---|
708 | case CONSOLE_GET_EVENT:
|
---|
709 | console_serialize_end();
|
---|
710 | cons_get_event(cons, callid, &call);
|
---|
711 | console_serialize_start();
|
---|
712 | continue;
|
---|
713 | case CONSOLE_KCON_ENABLE:
|
---|
714 | change_console(kernel_console);
|
---|
715 | break;
|
---|
716 | }
|
---|
717 | async_answer_3(callid, EOK, arg1, arg2, arg3);
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
722 | {
|
---|
723 | change_console(prev_console);
|
---|
724 | }
|
---|
725 |
|
---|
726 | static async_sess_t *connect_input(const char *svc_path)
|
---|
727 | {
|
---|
728 | async_sess_t *sess;
|
---|
729 | async_exch_t *exch;
|
---|
730 | service_id_t service_id;
|
---|
731 |
|
---|
732 | int rc = loc_service_get_id(svc_path, &service_id, 0);
|
---|
733 | if (rc == EOK) {
|
---|
734 | sess = loc_service_connect(EXCHANGE_ATOMIC, service_id, 0);
|
---|
735 | if (sess == NULL) {
|
---|
736 | printf("%s: Failed to connect to input server\n", NAME);
|
---|
737 | return NULL;
|
---|
738 | }
|
---|
739 | } else {
|
---|
740 | return NULL;
|
---|
741 | }
|
---|
742 |
|
---|
743 | exch = async_exchange_begin(sess);
|
---|
744 | if (exch == NULL) {
|
---|
745 | printf("%s: Failed to create callback from input server.\n", NAME);
|
---|
746 | return NULL;
|
---|
747 | }
|
---|
748 |
|
---|
749 | /* NB: The callback connection is slotted for removal */
|
---|
750 | rc = async_connect_to_me(exch, 0, 0, 0, input_events, NULL);
|
---|
751 |
|
---|
752 | async_exchange_end(exch);
|
---|
753 |
|
---|
754 | if (rc != EOK) {
|
---|
755 | async_hangup(sess);
|
---|
756 | printf("%s: Failed to create callback from input server (%s).\n",
|
---|
757 | NAME, str_error(rc));
|
---|
758 | return NULL;
|
---|
759 | }
|
---|
760 |
|
---|
761 | return sess;
|
---|
762 | }
|
---|
763 |
|
---|
764 | static bool console_srv_init(char *input_dev)
|
---|
765 | {
|
---|
766 | /* Connect to input server */
|
---|
767 | input_sess = connect_input(input_dev);
|
---|
768 | if (input_sess == NULL)
|
---|
769 | return false;
|
---|
770 |
|
---|
771 | /* Connect to framebuffer driver */
|
---|
772 | fb_info.phone = service_obsolete_connect_blocking(SERVICE_VIDEO, 0, 0);
|
---|
773 | if (fb_info.phone < 0) {
|
---|
774 | printf("%s: Failed to connect to video service\n", NAME);
|
---|
775 | return false;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /* Register server */
|
---|
779 | int rc = loc_server_register(NAME, client_connection);
|
---|
780 | if (rc < 0) {
|
---|
781 | printf("%s: Unable to register server (%d)\n", NAME, rc);
|
---|
782 | return false;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /* Initialize gcons */
|
---|
786 | gcons_init(fb_info.phone);
|
---|
787 |
|
---|
788 | /* Synchronize, the gcons could put something in queue */
|
---|
789 | async_obsolete_req_0_0(fb_info.phone, FB_FLUSH);
|
---|
790 | async_obsolete_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
|
---|
791 | async_obsolete_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
|
---|
792 |
|
---|
793 | /* Set up shared memory buffer. */
|
---|
794 | size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
|
---|
795 | interbuffer = as_get_mappable_page(ib_size);
|
---|
796 |
|
---|
797 | if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
|
---|
798 | AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
|
---|
799 | interbuffer = NULL;
|
---|
800 |
|
---|
801 | if (interbuffer) {
|
---|
802 | if (async_obsolete_share_out_start(fb_info.phone, interbuffer,
|
---|
803 | AS_AREA_READ) != EOK) {
|
---|
804 | as_area_destroy(interbuffer);
|
---|
805 | interbuffer = NULL;
|
---|
806 | }
|
---|
807 | }
|
---|
808 |
|
---|
809 | fb_pending.cnt = 0;
|
---|
810 |
|
---|
811 | /* Inititalize consoles */
|
---|
812 | size_t i;
|
---|
813 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
---|
814 | if (i != KERNEL_CONSOLE) {
|
---|
815 | if (screenbuffer_init(&consoles[i].scr,
|
---|
816 | fb_info.cols, fb_info.rows) == NULL) {
|
---|
817 | printf("%s: Unable to allocate screen buffer %zu\n", NAME, i);
|
---|
818 | return false;
|
---|
819 | }
|
---|
820 | screenbuffer_clear(&consoles[i].scr);
|
---|
821 | keybuffer_init(&consoles[i].keybuffer);
|
---|
822 | consoles[i].index = i;
|
---|
823 | consoles[i].refcount = 0;
|
---|
824 |
|
---|
825 | char vc[LOC_NAME_MAXLEN + 1];
|
---|
826 | snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
|
---|
827 |
|
---|
828 | if (loc_service_register(vc, &consoles[i].service_id) != EOK) {
|
---|
829 | printf("%s: Unable to register service %s\n", NAME, vc);
|
---|
830 | return false;
|
---|
831 | }
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | /* Disable kernel output to the console */
|
---|
836 | __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
|
---|
837 |
|
---|
838 | /* Initialize the screen */
|
---|
839 | console_serialize_start();
|
---|
840 | gcons_redraw_console();
|
---|
841 | set_style(STYLE_NORMAL);
|
---|
842 | screen_clear();
|
---|
843 | curs_goto(0, 0);
|
---|
844 | curs_visibility(active_console->scr.is_cursor_visible);
|
---|
845 | console_serialize_end();
|
---|
846 |
|
---|
847 | /* Receive kernel notifications */
|
---|
848 | async_set_interrupt_received(interrupt_received);
|
---|
849 | if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
|
---|
850 | printf("%s: Error registering kconsole notifications\n", NAME);
|
---|
851 |
|
---|
852 | return true;
|
---|
853 | }
|
---|
854 |
|
---|
855 | static void usage(void)
|
---|
856 | {
|
---|
857 | printf("Usage: console <input_dev>\n");
|
---|
858 | }
|
---|
859 |
|
---|
860 | int main(int argc, char *argv[])
|
---|
861 | {
|
---|
862 | if (argc < 2) {
|
---|
863 | usage();
|
---|
864 | return -1;
|
---|
865 | }
|
---|
866 |
|
---|
867 | printf(NAME ": HelenOS Console service\n");
|
---|
868 |
|
---|
869 | if (!console_srv_init(argv[1]))
|
---|
870 | return -1;
|
---|
871 |
|
---|
872 | printf(NAME ": Accepting connections\n");
|
---|
873 | async_manager();
|
---|
874 |
|
---|
875 | return 0;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /** @}
|
---|
879 | */
|
---|