Rev 3767 | Rev 3844 | 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> |
1694 | palkovsky | 38 | #include <keys.h> |
1451 | cejka | 39 | #include <ipc/fb.h> |
1445 | cejka | 40 | #include <ipc/services.h> |
41 | #include <errno.h> |
||
1451 | cejka | 42 | #include <key_buffer.h> |
3747 | svoboda | 43 | #include <ipc/console.h> |
1453 | palkovsky | 44 | #include <unistd.h> |
45 | #include <async.h> |
||
1481 | cejka | 46 | #include <libadt/fifo.h> |
1487 | cejka | 47 | #include <screenbuffer.h> |
1867 | jermar | 48 | #include <sys/mman.h> |
3084 | decky | 49 | #include <stdio.h> |
3761 | decky | 50 | #include <sysinfo.h> |
1445 | cejka | 51 | |
3767 | svoboda | 52 | #include "console.h" |
1522 | palkovsky | 53 | #include "gcons.h" |
54 | |||
1481 | cejka | 55 | #define MAX_KEYREQUESTS_BUFFERED 32 |
1451 | cejka | 56 | |
3084 | decky | 57 | #define NAME "console" |
1445 | cejka | 58 | |
1526 | cejka | 59 | /** Index of currently used virtual console. |
60 | */ |
||
1512 | cejka | 61 | int active_console = 0; |
3761 | decky | 62 | int prev_console = 0; |
1453 | palkovsky | 63 | |
1526 | cejka | 64 | /** Information about framebuffer |
65 | */ |
||
1487 | cejka | 66 | struct { |
67 | int phone; /**< Framebuffer phone */ |
||
1506 | cejka | 68 | ipcarg_t rows; /**< Framebuffer rows */ |
69 | ipcarg_t cols; /**< Framebuffer columns */ |
||
1487 | cejka | 70 | } fb_info; |
71 | |||
1451 | cejka | 72 | typedef struct { |
1526 | cejka | 73 | keybuffer_t keybuffer; /**< Buffer for incoming keys. */ |
2025 | jermar | 74 | /** Buffer for unsatisfied request for keys. */ |
75 | FIFO_CREATE_STATIC(keyrequests, ipc_callid_t, |
||
76 | MAX_KEYREQUESTS_BUFFERED); |
||
1526 | cejka | 77 | int keyrequest_counter; /**< Number of requests in buffer. */ |
78 | int client_phone; /**< Phone to connected client. */ |
||
2025 | jermar | 79 | int used; /**< 1 if this virtual console is |
80 | * connected to some client.*/ |
||
81 | screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen |
||
82 | * contents and related settings. */ |
||
1451 | cejka | 83 | } connection_t; |
84 | |||
2025 | jermar | 85 | static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual |
86 | * consoles */ |
||
87 | static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared |
||
88 | * with framebufer used for |
||
89 | * faster virtual console |
||
2069 | jermar | 90 | * switching */ |
1506 | cejka | 91 | |
92 | |||
1526 | cejka | 93 | /** Find unused virtual console. |
94 | * |
||
95 | */ |
||
1574 | palkovsky | 96 | static int find_free_connection(void) |
1451 | cejka | 97 | { |
2069 | jermar | 98 | int i; |
1451 | cejka | 99 | |
2069 | jermar | 100 | for (i = 0; i < CONSOLE_COUNT; i++) { |
1574 | palkovsky | 101 | if (!connections[i].used) |
1451 | cejka | 102 | return i; |
103 | } |
||
1574 | palkovsky | 104 | return -1; |
1451 | cejka | 105 | } |
106 | |||
1552 | palkovsky | 107 | static void clrscr(void) |
108 | { |
||
2621 | jermar | 109 | async_msg_0(fb_info.phone, FB_CLEAR); |
1552 | palkovsky | 110 | } |
111 | |||
3735 | decky | 112 | static void curs_visibility(bool visible) |
1552 | palkovsky | 113 | { |
3735 | decky | 114 | async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); |
1552 | palkovsky | 115 | } |
116 | |||
3735 | decky | 117 | static void curs_hide_sync(void) |
118 | { |
||
119 | ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); |
||
120 | } |
||
121 | |||
1552 | palkovsky | 122 | static void curs_goto(int row, int col) |
123 | { |
||
1610 | palkovsky | 124 | async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col); |
1552 | palkovsky | 125 | } |
126 | |||
3767 | svoboda | 127 | static void set_style(int style) |
1552 | palkovsky | 128 | { |
3767 | svoboda | 129 | async_msg_1(fb_info.phone, FB_SET_STYLE, style); |
1552 | palkovsky | 130 | } |
131 | |||
3767 | svoboda | 132 | static void set_color(int fgcolor, int bgcolor, int flags) |
1552 | palkovsky | 133 | { |
3767 | svoboda | 134 | async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags); |
1552 | palkovsky | 135 | } |
136 | |||
3767 | svoboda | 137 | static void set_rgb_color(int fgcolor, int bgcolor) |
138 | { |
||
139 | async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor); |
||
140 | } |
||
141 | |||
142 | static void set_attrs(attrs_t *attrs) |
||
143 | { |
||
144 | switch (attrs->t) { |
||
145 | case at_style: |
||
146 | set_style(attrs->a.s.style); |
||
147 | break; |
||
148 | |||
149 | case at_idx: |
||
150 | set_color(attrs->a.i.fg_color, attrs->a.i.bg_color, |
||
151 | attrs->a.i.flags); |
||
152 | break; |
||
153 | |||
154 | case at_rgb: |
||
155 | set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color); |
||
156 | break; |
||
157 | } |
||
158 | } |
||
159 | |||
1552 | palkovsky | 160 | static void prtchr(char c, int row, int col) |
161 | { |
||
1610 | palkovsky | 162 | async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col); |
1552 | palkovsky | 163 | } |
164 | |||
1497 | cejka | 165 | /** Check key and process special keys. |
166 | * |
||
2566 | jermar | 167 | * |
168 | */ |
||
1497 | cejka | 169 | static void write_char(int console, char key) |
170 | { |
||
171 | screenbuffer_t *scr = &(connections[console].screenbuffer); |
||
172 | |||
173 | switch (key) { |
||
2025 | jermar | 174 | case '\n': |
2069 | jermar | 175 | scr->position_y++; |
176 | scr->position_x = 0; |
||
2025 | jermar | 177 | break; |
178 | case '\r': |
||
179 | break; |
||
180 | case '\t': |
||
181 | scr->position_x += 8; |
||
182 | scr->position_x -= scr->position_x % 8; |
||
183 | break; |
||
184 | case '\b': |
||
185 | if (scr->position_x == 0) |
||
1497 | cejka | 186 | break; |
2025 | jermar | 187 | scr->position_x--; |
188 | if (console == active_console) |
||
189 | prtchr(' ', scr->position_y, scr->position_x); |
||
190 | screenbuffer_putchar(scr, ' '); |
||
191 | break; |
||
192 | default: |
||
193 | if (console == active_console) |
||
194 | prtchr(key, scr->position_y, scr->position_x); |
||
1497 | cejka | 195 | |
2025 | jermar | 196 | screenbuffer_putchar(scr, key); |
197 | scr->position_x++; |
||
1497 | cejka | 198 | } |
199 | |||
200 | scr->position_y += (scr->position_x >= scr->size_x); |
||
201 | |||
202 | if (scr->position_y >= scr->size_y) { |
||
203 | scr->position_y = scr->size_y - 1; |
||
1674 | palkovsky | 204 | screenbuffer_clear_line(scr, scr->top_line); |
2069 | jermar | 205 | scr->top_line = (scr->top_line + 1) % scr->size_y; |
1518 | palkovsky | 206 | if (console == active_console) |
2621 | jermar | 207 | async_msg_1(fb_info.phone, FB_SCROLL, 1); |
1497 | cejka | 208 | } |
209 | |||
210 | scr->position_x = scr->position_x % scr->size_x; |
||
211 | |||
1552 | palkovsky | 212 | if (console == active_console) |
213 | curs_goto(scr->position_y, scr->position_x); |
||
1504 | cejka | 214 | |
1497 | cejka | 215 | } |
216 | |||
1552 | palkovsky | 217 | /** Switch to new console */ |
218 | static void change_console(int newcons) |
||
219 | { |
||
220 | connection_t *conn; |
||
1673 | palkovsky | 221 | int i, j, rc; |
1578 | cejka | 222 | keyfield_t *field; |
3767 | svoboda | 223 | attrs_t *attrs; |
3707 | decky | 224 | |
1552 | palkovsky | 225 | if (newcons == active_console) |
226 | return; |
||
3707 | decky | 227 | |
1555 | palkovsky | 228 | if (newcons == KERNEL_CONSOLE) { |
3707 | decky | 229 | async_serialize_start(); |
3735 | decky | 230 | curs_hide_sync(); |
3707 | decky | 231 | gcons_in_kernel(); |
232 | async_serialize_end(); |
||
233 | |||
3761 | decky | 234 | if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) { |
235 | prev_console = active_console; |
||
3707 | decky | 236 | active_console = KERNEL_CONSOLE; |
3761 | decky | 237 | } else |
3735 | decky | 238 | newcons = active_console; |
3707 | decky | 239 | } |
240 | |||
241 | if (newcons != KERNEL_CONSOLE) { |
||
242 | async_serialize_start(); |
||
243 | |||
1555 | palkovsky | 244 | if (active_console == KERNEL_CONSOLE) |
3707 | decky | 245 | gcons_redraw_console(); |
246 | |||
247 | active_console = newcons; |
||
248 | gcons_change_console(newcons); |
||
249 | conn = &connections[active_console]; |
||
250 | |||
3767 | svoboda | 251 | set_attrs(&conn->screenbuffer.attrs); |
3735 | decky | 252 | curs_visibility(false); |
3707 | decky | 253 | if (interbuffer) { |
254 | for (i = 0; i < conn->screenbuffer.size_x; i++) |
||
255 | for (j = 0; j < conn->screenbuffer.size_y; j++) { |
||
256 | unsigned int size_x; |
||
257 | |||
258 | size_x = conn->screenbuffer.size_x; |
||
259 | interbuffer[i + j * size_x] = |
||
260 | *get_field_at(&conn->screenbuffer, i, j); |
||
261 | } |
||
262 | /* This call can preempt, but we are already at the end */ |
||
3761 | decky | 263 | rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA); |
3707 | decky | 264 | } |
265 | |||
266 | if ((!interbuffer) || (rc != 0)) { |
||
3767 | svoboda | 267 | set_attrs(&conn->screenbuffer.attrs); |
1552 | palkovsky | 268 | clrscr(); |
3767 | svoboda | 269 | attrs = &conn->screenbuffer.attrs; |
3707 | decky | 270 | |
3761 | decky | 271 | for (j = 0; j < conn->screenbuffer.size_y; j++) |
3707 | decky | 272 | for (i = 0; i < conn->screenbuffer.size_x; i++) { |
273 | field = get_field_at(&conn->screenbuffer, i, j); |
||
3767 | svoboda | 274 | if (!attrs_same(*attrs, field->attrs)) |
275 | set_attrs(&field->attrs); |
||
276 | attrs = &field->attrs; |
||
3707 | decky | 277 | if ((field->character == ' ') && |
3767 | svoboda | 278 | (attrs_same(field->attrs, |
279 | conn->screenbuffer.attrs))) |
||
3707 | decky | 280 | continue; |
3767 | svoboda | 281 | |
3707 | decky | 282 | prtchr(field->character, j, i); |
283 | } |
||
1552 | palkovsky | 284 | } |
3707 | decky | 285 | |
286 | curs_goto(conn->screenbuffer.position_y, |
||
287 | conn->screenbuffer.position_x); |
||
288 | curs_visibility(conn->screenbuffer.is_cursor_visible); |
||
289 | |||
1717 | palkovsky | 290 | async_serialize_end(); |
1552 | palkovsky | 291 | } |
292 | } |
||
293 | |||
1526 | cejka | 294 | /** Handler for keyboard */ |
1453 | palkovsky | 295 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall) |
1445 | cejka | 296 | { |
1453 | palkovsky | 297 | ipc_callid_t callid; |
1445 | cejka | 298 | ipc_call_t call; |
1453 | palkovsky | 299 | int retval; |
1560 | vana | 300 | int c; |
1489 | palkovsky | 301 | connection_t *conn; |
1717 | palkovsky | 302 | int newcon; |
1506 | cejka | 303 | |
1453 | palkovsky | 304 | /* Ignore parameters, the connection is alread opened */ |
305 | while (1) { |
||
306 | callid = async_get_call(&call); |
||
307 | switch (IPC_GET_METHOD(call)) { |
||
308 | case IPC_M_PHONE_HUNGUP: |
||
309 | /* TODO: Handle hangup */ |
||
310 | return; |
||
1717 | palkovsky | 311 | case KBD_MS_LEFT: |
312 | newcon = gcons_mouse_btn(IPC_GET_ARG1(call)); |
||
313 | if (newcon != -1) |
||
314 | change_console(newcon); |
||
1721 | palkovsky | 315 | retval = 0; |
1717 | palkovsky | 316 | break; |
1707 | palkovsky | 317 | case KBD_MS_MOVE: |
2025 | jermar | 318 | gcons_mouse_move(IPC_GET_ARG1(call), |
2539 | jermar | 319 | IPC_GET_ARG2(call)); |
1721 | palkovsky | 320 | retval = 0; |
1707 | palkovsky | 321 | break; |
1453 | palkovsky | 322 | case KBD_PUSHCHAR: |
323 | /* got key from keyboard driver */ |
||
324 | retval = 0; |
||
1520 | cejka | 325 | c = IPC_GET_ARG1(call); |
1453 | palkovsky | 326 | /* switch to another virtual console */ |
1481 | cejka | 327 | |
1489 | palkovsky | 328 | conn = &connections[active_console]; |
2025 | jermar | 329 | /* |
330 | * if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + |
||
331 | * CONSOLE_COUNT)) { |
||
332 | */ |
||
1560 | vana | 333 | if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) { |
334 | if (c == 0x112) |
||
1555 | palkovsky | 335 | change_console(KERNEL_CONSOLE); |
1552 | palkovsky | 336 | else |
1560 | vana | 337 | change_console(c - 0x101); |
1453 | palkovsky | 338 | break; |
339 | } |
||
1481 | cejka | 340 | |
341 | /* if client is awaiting key, send it */ |
||
1489 | palkovsky | 342 | if (conn->keyrequest_counter > 0) { |
343 | conn->keyrequest_counter--; |
||
2619 | jermar | 344 | ipc_answer_1(fifo_pop(conn->keyrequests), EOK, |
345 | c); |
||
1481 | cejka | 346 | break; |
347 | } |
||
348 | |||
1489 | palkovsky | 349 | keybuffer_push(&conn->keybuffer, c); |
1721 | palkovsky | 350 | retval = 0; |
1476 | cejka | 351 | |
1453 | palkovsky | 352 | break; |
353 | default: |
||
1459 | palkovsky | 354 | retval = ENOENT; |
1610 | palkovsky | 355 | } |
2619 | jermar | 356 | ipc_answer_0(callid, retval); |
1453 | palkovsky | 357 | } |
358 | } |
||
359 | |||
360 | /** Default thread for new connections */ |
||
1518 | palkovsky | 361 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall) |
1453 | palkovsky | 362 | { |
1445 | cejka | 363 | ipc_callid_t callid; |
1453 | palkovsky | 364 | ipc_call_t call; |
365 | int consnum; |
||
3767 | svoboda | 366 | ipcarg_t arg1, arg2, arg3; |
1592 | palkovsky | 367 | connection_t *conn; |
3761 | decky | 368 | |
1574 | palkovsky | 369 | if ((consnum = find_free_connection()) == -1) { |
2619 | jermar | 370 | ipc_answer_0(iid, ELIMIT); |
1453 | palkovsky | 371 | return; |
372 | } |
||
1592 | palkovsky | 373 | conn = &connections[consnum]; |
1610 | palkovsky | 374 | conn->used = 1; |
1555 | palkovsky | 375 | |
1610 | palkovsky | 376 | async_serialize_start(); |
1555 | palkovsky | 377 | gcons_notify_connect(consnum); |
2637 | cejka | 378 | conn->client_phone = IPC_GET_ARG5(*icall); |
1592 | palkovsky | 379 | screenbuffer_clear(&conn->screenbuffer); |
1497 | cejka | 380 | |
1453 | palkovsky | 381 | /* Accept the connection */ |
2619 | jermar | 382 | ipc_answer_0(iid, EOK); |
3761 | decky | 383 | |
1453 | palkovsky | 384 | while (1) { |
1610 | palkovsky | 385 | async_serialize_end(); |
1453 | palkovsky | 386 | callid = async_get_call(&call); |
1610 | palkovsky | 387 | async_serialize_start(); |
3761 | decky | 388 | |
2566 | jermar | 389 | arg1 = 0; |
390 | arg2 = 0; |
||
1453 | palkovsky | 391 | switch (IPC_GET_METHOD(call)) { |
392 | case IPC_M_PHONE_HUNGUP: |
||
1592 | palkovsky | 393 | gcons_notify_disconnect(consnum); |
1610 | palkovsky | 394 | |
1592 | palkovsky | 395 | /* Answer all pending requests */ |
3761 | decky | 396 | while (conn->keyrequest_counter > 0) { |
1592 | palkovsky | 397 | conn->keyrequest_counter--; |
2619 | jermar | 398 | ipc_answer_0(fifo_pop(conn->keyrequests), |
399 | ENOENT); |
||
1592 | palkovsky | 400 | break; |
401 | } |
||
1616 | palkovsky | 402 | conn->used = 0; |
1453 | palkovsky | 403 | return; |
404 | case CONSOLE_PUTCHAR: |
||
1497 | cejka | 405 | write_char(consnum, IPC_GET_ARG1(call)); |
1528 | palkovsky | 406 | gcons_notify_char(consnum); |
1453 | palkovsky | 407 | break; |
1476 | cejka | 408 | case CONSOLE_CLEAR: |
1487 | cejka | 409 | /* Send message to fb */ |
410 | if (consnum == active_console) { |
||
2621 | jermar | 411 | async_msg_0(fb_info.phone, FB_CLEAR); |
1487 | cejka | 412 | } |
413 | |||
1592 | palkovsky | 414 | screenbuffer_clear(&conn->screenbuffer); |
1487 | cejka | 415 | |
1476 | cejka | 416 | break; |
417 | case CONSOLE_GOTO: |
||
2025 | jermar | 418 | screenbuffer_goto(&conn->screenbuffer, |
2539 | jermar | 419 | IPC_GET_ARG2(call), IPC_GET_ARG1(call)); |
1567 | palkovsky | 420 | if (consnum == active_console) |
2025 | jermar | 421 | curs_goto(IPC_GET_ARG1(call), |
2539 | jermar | 422 | IPC_GET_ARG2(call)); |
1476 | cejka | 423 | break; |
1521 | cejka | 424 | case CONSOLE_GETSIZE: |
1528 | palkovsky | 425 | arg1 = fb_info.rows; |
426 | arg2 = fb_info.cols; |
||
1521 | cejka | 427 | break; |
1523 | cejka | 428 | case CONSOLE_FLUSH: |
1723 | palkovsky | 429 | if (consnum == active_console) |
2621 | jermar | 430 | async_req_0_0(fb_info.phone, FB_FLUSH); |
1523 | cejka | 431 | break; |
1525 | cejka | 432 | case CONSOLE_SET_STYLE: |
433 | arg1 = IPC_GET_ARG1(call); |
||
3767 | svoboda | 434 | screenbuffer_set_style(&conn->screenbuffer, arg1); |
435 | if (consnum == active_console) |
||
436 | set_style(arg1); |
||
437 | break; |
||
438 | case CONSOLE_SET_COLOR: |
||
439 | arg1 = IPC_GET_ARG1(call); |
||
1525 | cejka | 440 | arg2 = IPC_GET_ARG2(call); |
3767 | svoboda | 441 | arg3 = IPC_GET_ARG3(call); |
442 | screenbuffer_set_color(&conn->screenbuffer, arg1, |
||
443 | arg2, arg3); |
||
444 | if (consnum == active_console) |
||
445 | set_color(arg1, arg2, arg3); |
||
446 | break; |
||
447 | case CONSOLE_SET_RGB_COLOR: |
||
448 | arg1 = IPC_GET_ARG1(call); |
||
449 | arg2 = IPC_GET_ARG2(call); |
||
450 | screenbuffer_set_rgb_color(&conn->screenbuffer, arg1, |
||
2539 | jermar | 451 | arg2); |
1525 | cejka | 452 | if (consnum == active_console) |
3767 | svoboda | 453 | set_rgb_color(arg1, arg2); |
1525 | cejka | 454 | break; |
1575 | cejka | 455 | case CONSOLE_CURSOR_VISIBILITY: |
456 | arg1 = IPC_GET_ARG1(call); |
||
1592 | palkovsky | 457 | conn->screenbuffer.is_cursor_visible = arg1; |
1575 | cejka | 458 | if (consnum == active_console) |
459 | curs_visibility(arg1); |
||
460 | break; |
||
1453 | palkovsky | 461 | case CONSOLE_GETCHAR: |
1592 | palkovsky | 462 | if (keybuffer_empty(&conn->keybuffer)) { |
1481 | cejka | 463 | /* buffer is empty -> store request */ |
2025 | jermar | 464 | if (conn->keyrequest_counter < |
465 | MAX_KEYREQUESTS_BUFFERED) { |
||
1592 | palkovsky | 466 | fifo_push(conn->keyrequests, callid); |
467 | conn->keyrequest_counter++; |
||
1481 | cejka | 468 | } else { |
2025 | jermar | 469 | /* |
470 | * No key available and too many |
||
471 | * requests => fail. |
||
472 | */ |
||
2619 | jermar | 473 | ipc_answer_0(callid, ELIMIT); |
1481 | cejka | 474 | } |
475 | continue; |
||
2025 | jermar | 476 | } |
3599 | jermar | 477 | int ch; |
478 | keybuffer_pop(&conn->keybuffer, &ch); |
||
479 | arg1 = ch; |
||
1453 | palkovsky | 480 | break; |
481 | } |
||
2619 | jermar | 482 | ipc_answer_2(callid, EOK, arg1, arg2); |
1453 | palkovsky | 483 | } |
484 | } |
||
485 | |||
3761 | decky | 486 | static void interrupt_received(ipc_callid_t callid, ipc_call_t *call) |
487 | { |
||
488 | change_console(prev_console); |
||
489 | } |
||
490 | |||
1453 | palkovsky | 491 | int main(int argc, char *argv[]) |
492 | { |
||
3084 | decky | 493 | printf(NAME ": HelenOS Console service\n"); |
494 | |||
1453 | palkovsky | 495 | ipcarg_t phonehash; |
1721 | palkovsky | 496 | int kbd_phone; |
3791 | svoboda | 497 | size_t ib_size; |
1451 | cejka | 498 | int i; |
3761 | decky | 499 | |
1490 | palkovsky | 500 | async_set_client_connection(client_connection); |
1445 | cejka | 501 | |
502 | /* Connect to keyboard driver */ |
||
3761 | decky | 503 | |
2635 | cejka | 504 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
2069 | jermar | 505 | while (kbd_phone < 0) { |
1453 | palkovsky | 506 | usleep(10000); |
2635 | cejka | 507 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
2025 | jermar | 508 | } |
1445 | cejka | 509 | |
2637 | cejka | 510 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
1445 | cejka | 511 | return -1; |
1689 | palkovsky | 512 | async_new_connection(phonehash, 0, NULL, keyboard_events); |
513 | |||
1445 | cejka | 514 | /* Connect to framebuffer driver */ |
2635 | cejka | 515 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
2069 | jermar | 516 | while (fb_info.phone < 0) { |
1487 | cejka | 517 | usleep(10000); |
2635 | cejka | 518 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
1487 | cejka | 519 | } |
1552 | palkovsky | 520 | |
1522 | palkovsky | 521 | /* Initialize gcons */ |
522 | gcons_init(fb_info.phone); |
||
523 | /* Synchronize, the gcons can have something in queue */ |
||
2621 | jermar | 524 | async_req_0_0(fb_info.phone, FB_FLUSH); |
1487 | cejka | 525 | |
2621 | jermar | 526 | async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows, |
2539 | jermar | 527 | &fb_info.cols); |
3767 | svoboda | 528 | set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND); |
1552 | palkovsky | 529 | clrscr(); |
1487 | cejka | 530 | |
531 | /* Init virtual consoles */ |
||
1451 | cejka | 532 | for (i = 0; i < CONSOLE_COUNT; i++) { |
533 | connections[i].used = 0; |
||
2539 | jermar | 534 | keybuffer_init(&connections[i].keybuffer); |
1481 | cejka | 535 | |
2539 | jermar | 536 | connections[i].keyrequests.head = 0; |
537 | connections[i].keyrequests.tail = 0; |
||
1481 | cejka | 538 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED; |
539 | connections[i].keyrequest_counter = 0; |
||
1487 | cejka | 540 | |
2539 | jermar | 541 | if (screenbuffer_init(&connections[i].screenbuffer, |
542 | fb_info.cols, fb_info.rows) == NULL) { |
||
2069 | jermar | 543 | /* FIXME: handle error */ |
1487 | cejka | 544 | return -1; |
545 | } |
||
1451 | cejka | 546 | } |
1574 | palkovsky | 547 | connections[KERNEL_CONSOLE].used = 1; |
3791 | svoboda | 548 | |
549 | /* Set up shared memory buffer. */ |
||
550 | ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows; |
||
551 | interbuffer = as_get_mappable_page(ib_size); |
||
552 | |||
553 | if (as_area_create(interbuffer, ib_size, AS_AREA_READ | |
||
554 | AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) { |
||
555 | interbuffer = NULL; |
||
556 | } |
||
557 | |||
558 | if (interbuffer) { |
||
2678 | jermar | 559 | if (ipc_share_out_start(fb_info.phone, interbuffer, |
2677 | jermar | 560 | AS_AREA_READ) != EOK) { |
3791 | svoboda | 561 | as_area_destroy(interbuffer); |
1512 | cejka | 562 | interbuffer = NULL; |
563 | } |
||
564 | } |
||
3761 | decky | 565 | |
2069 | jermar | 566 | curs_goto(0, 0); |
2539 | jermar | 567 | curs_visibility( |
568 | connections[active_console].screenbuffer.is_cursor_visible); |
||
3761 | decky | 569 | |
1518 | palkovsky | 570 | /* Register at NS */ |
3084 | decky | 571 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
1445 | cejka | 572 | return -1; |
573 | |||
3761 | decky | 574 | /* Receive kernel notifications */ |
575 | if (sysinfo_value("kconsole.present")) { |
||
576 | int devno = sysinfo_value("kconsole.devno"); |
||
577 | int inr = sysinfo_value("kconsole.inr"); |
||
578 | if (ipc_register_irq(inr, devno, 0, NULL) != EOK) |
||
579 | printf(NAME ": Error registering kconsole notifications\n"); |
||
580 | |||
581 | async_set_interrupt_received(interrupt_received); |
||
582 | } |
||
583 | |||
3084 | decky | 584 | // FIXME: avoid connectiong to itself, keep using klog |
585 | // printf(NAME ": Accepting connections\n"); |
||
1453 | palkovsky | 586 | async_manager(); |
3761 | decky | 587 | |
588 | return 0; |
||
1445 | cejka | 589 | } |
1649 | cejka | 590 | |
591 | /** @} |
||
592 | */ |