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