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