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