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