[51c1b003] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Josef Cejka
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #include <kbd.h>
|
---|
[79460ae] | 31 | #include <fb.h>
|
---|
[51c1b003] | 32 | #include <ipc/ipc.h>
|
---|
[79460ae] | 33 | #include <ipc/fb.h>
|
---|
[51c1b003] | 34 | #include <ipc/services.h>
|
---|
| 35 | #include <errno.h>
|
---|
[79460ae] | 36 | #include <key_buffer.h>
|
---|
| 37 | #include <console.h>
|
---|
[eaf34f7] | 38 | #include <unistd.h>
|
---|
| 39 | #include <async.h>
|
---|
[cf28036c] | 40 | #include <libadt/fifo.h>
|
---|
[3993b3d] | 41 | #include <screenbuffer.h>
|
---|
[79460ae] | 42 |
|
---|
[10569b1] | 43 | #define CONSOLE_COUNT 12
|
---|
[cf28036c] | 44 | #define MAX_KEYREQUESTS_BUFFERED 32
|
---|
[51c1b003] | 45 |
|
---|
| 46 | #define NAME "CONSOLE"
|
---|
| 47 |
|
---|
[ad123964] | 48 | int active_console = 1;
|
---|
[eaf34f7] | 49 |
|
---|
[3993b3d] | 50 | struct {
|
---|
| 51 | int phone; /**< Framebuffer phone */
|
---|
| 52 | int rows; /**< Framebuffer rows */
|
---|
| 53 | int cols; /**< Framebuffer columns */
|
---|
| 54 | } fb_info;
|
---|
| 55 |
|
---|
[79460ae] | 56 | typedef struct {
|
---|
| 57 | keybuffer_t keybuffer;
|
---|
[cf28036c] | 58 | FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
|
---|
| 59 | int keyrequest_counter;
|
---|
[79460ae] | 60 | int client_phone;
|
---|
| 61 | int used;
|
---|
[3993b3d] | 62 | screenbuffer_t screenbuffer;
|
---|
[79460ae] | 63 | } connection_t;
|
---|
| 64 |
|
---|
| 65 | connection_t connections[CONSOLE_COUNT];
|
---|
| 66 |
|
---|
| 67 | static int find_free_connection()
|
---|
| 68 | {
|
---|
| 69 | int i = 0;
|
---|
| 70 |
|
---|
| 71 | while (i < CONSOLE_COUNT) {
|
---|
| 72 | if (connections[i].used == 0)
|
---|
| 73 | return i;
|
---|
| 74 | ++i;
|
---|
| 75 | }
|
---|
| 76 | return CONSOLE_COUNT;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | static int find_connection(int client_phone)
|
---|
| 81 | {
|
---|
| 82 | int i = 0;
|
---|
| 83 |
|
---|
| 84 | while (i < CONSOLE_COUNT) {
|
---|
| 85 | if (connections[i].client_phone == client_phone)
|
---|
| 86 | return i;
|
---|
| 87 | ++i;
|
---|
| 88 | }
|
---|
| 89 | return CONSOLE_COUNT;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[10569b1] | 92 | /** Check key and process special keys.
|
---|
| 93 | *
|
---|
| 94 | * */
|
---|
| 95 | static void write_char(int console, char key)
|
---|
| 96 | {
|
---|
| 97 | screenbuffer_t *scr = &(connections[console].screenbuffer);
|
---|
| 98 |
|
---|
| 99 | switch (key) {
|
---|
| 100 | case '\n':
|
---|
| 101 | scr->position_y += 1;
|
---|
| 102 | scr->position_x = 0;
|
---|
| 103 | break;
|
---|
| 104 | case '\r':
|
---|
| 105 | break;
|
---|
| 106 | case '\t':
|
---|
| 107 | scr->position_x += 8;
|
---|
| 108 | scr->position_x -= scr->position_x % 8;
|
---|
| 109 | break;
|
---|
| 110 | case '\b':
|
---|
| 111 | if (scr->position_x == 0)
|
---|
| 112 | break;
|
---|
| 113 |
|
---|
| 114 | scr->position_x--;
|
---|
| 115 |
|
---|
| 116 | if (console == active_console) {
|
---|
| 117 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x, NULL, NULL);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | screenbuffer_putchar(scr, ' ');
|
---|
| 121 |
|
---|
| 122 | break;
|
---|
| 123 | default:
|
---|
| 124 | if (console == active_console) {
|
---|
| 125 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x, NULL, NULL);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | screenbuffer_putchar(scr, key);
|
---|
| 129 | scr->position_x++;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | scr->position_y += (scr->position_x >= scr->size_x);
|
---|
| 133 |
|
---|
| 134 | if (scr->position_y >= scr->size_y) {
|
---|
| 135 | scr->position_y = scr->size_y - 1;
|
---|
| 136 | screenbuffer_clear_line(scr, scr->top_line++);
|
---|
| 137 | ipc_call_async(fb_info.phone, FB_SCROLL, 1, NULL, NULL);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | scr->position_x = scr->position_x % scr->size_x;
|
---|
[bd929cfb] | 141 |
|
---|
| 142 | if (console == active_console)
|
---|
| 143 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x, NULL, NULL);
|
---|
[10569b1] | 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[eaf34f7] | 148 | /* Handler for keyboard */
|
---|
| 149 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[51c1b003] | 150 | {
|
---|
[eaf34f7] | 151 | ipc_callid_t callid;
|
---|
[51c1b003] | 152 | ipc_call_t call;
|
---|
[eaf34f7] | 153 | int retval;
|
---|
[3993b3d] | 154 | int i, j;
|
---|
[c1d2c9d] | 155 | char c,d;
|
---|
| 156 | connection_t *conn;
|
---|
[eaf34f7] | 157 |
|
---|
| 158 | /* Ignore parameters, the connection is alread opened */
|
---|
| 159 | while (1) {
|
---|
| 160 | callid = async_get_call(&call);
|
---|
| 161 | switch (IPC_GET_METHOD(call)) {
|
---|
| 162 | case IPC_M_PHONE_HUNGUP:
|
---|
| 163 | ipc_answer_fast(callid,0,0,0);
|
---|
| 164 | /* TODO: Handle hangup */
|
---|
| 165 | return;
|
---|
| 166 | case KBD_PUSHCHAR:
|
---|
| 167 | /* got key from keyboard driver */
|
---|
[b27a97bb] | 168 |
|
---|
[eaf34f7] | 169 | retval = 0;
|
---|
[ad123964] | 170 | c = IPC_GET_ARG1(call);
|
---|
[eaf34f7] | 171 | /* switch to another virtual console */
|
---|
[cf28036c] | 172 |
|
---|
[c1d2c9d] | 173 | conn = &connections[active_console];
|
---|
[da0c91e7] | 174 | // if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
|
---|
| 175 | if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
|
---|
[3993b3d] | 176 | /*FIXME: draw another console content from buffer */
|
---|
[838c48e] | 177 | if (c - '1' == active_console)
|
---|
[10569b1] | 178 | break;
|
---|
[da0c91e7] | 179 | active_console = c - '1';
|
---|
[c1d2c9d] | 180 | conn = &connections[active_console];
|
---|
| 181 |
|
---|
| 182 | ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
|
---|
[3993b3d] | 183 | ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
|
---|
[10569b1] | 184 |
|
---|
[c1d2c9d] | 185 | for (i = 0; i < conn->screenbuffer.size_x; i++)
|
---|
| 186 | for (j = 0; j < conn->screenbuffer.size_y; j++) {
|
---|
| 187 | d = get_field_at(&(conn->screenbuffer),i, j)->character;
|
---|
| 188 | if (d && d != ' ')
|
---|
| 189 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
|
---|
| 190 | }
|
---|
[838c48e] | 191 |
|
---|
[c1d2c9d] | 192 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
|
---|
| 193 | ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
|
---|
[3993b3d] | 194 |
|
---|
[eaf34f7] | 195 | break;
|
---|
| 196 | }
|
---|
[cf28036c] | 197 |
|
---|
| 198 | /* if client is awaiting key, send it */
|
---|
[c1d2c9d] | 199 | if (conn->keyrequest_counter > 0) {
|
---|
| 200 | conn->keyrequest_counter--;
|
---|
| 201 | ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
|
---|
[cf28036c] | 202 | break;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /*FIXME: else store key to its buffer */
|
---|
[c1d2c9d] | 206 | keybuffer_push(&conn->keybuffer, c);
|
---|
[ad123964] | 207 |
|
---|
[eaf34f7] | 208 | break;
|
---|
| 209 | default:
|
---|
[1029d3d3] | 210 | retval = ENOENT;
|
---|
[eaf34f7] | 211 | }
|
---|
[1029d3d3] | 212 | ipc_answer_fast(callid, retval, 0, 0);
|
---|
[eaf34f7] | 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /** Default thread for new connections */
|
---|
| 217 | void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 218 | {
|
---|
[51c1b003] | 219 | ipc_callid_t callid;
|
---|
[eaf34f7] | 220 | ipc_call_t call;
|
---|
| 221 | int consnum;
|
---|
| 222 | ipcarg_t arg1;
|
---|
| 223 |
|
---|
| 224 | if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
|
---|
| 225 | ipc_answer_fast(iid,ELIMIT,0,0);
|
---|
| 226 | return;
|
---|
| 227 | }
|
---|
[10569b1] | 228 |
|
---|
[eaf34f7] | 229 | connections[consnum].used = 1;
|
---|
| 230 | connections[consnum].client_phone = IPC_GET_ARG3(call);
|
---|
[3993b3d] | 231 | screenbuffer_clear(&(connections[consnum].screenbuffer));
|
---|
[10569b1] | 232 |
|
---|
[eaf34f7] | 233 | /* Accept the connection */
|
---|
| 234 | ipc_answer_fast(iid,0,0,0);
|
---|
| 235 |
|
---|
| 236 | while (1) {
|
---|
| 237 | callid = async_get_call(&call);
|
---|
| 238 | switch (IPC_GET_METHOD(call)) {
|
---|
| 239 | case IPC_M_PHONE_HUNGUP:
|
---|
| 240 | /* TODO */
|
---|
| 241 | ipc_answer_fast(callid, 0,0,0);
|
---|
| 242 | return;
|
---|
| 243 | case CONSOLE_PUTCHAR:
|
---|
[10569b1] | 244 | write_char(consnum, IPC_GET_ARG1(call));
|
---|
[ad123964] | 245 | break;
|
---|
| 246 | case CONSOLE_CLEAR:
|
---|
[3993b3d] | 247 | /* Send message to fb */
|
---|
| 248 | if (consnum == active_console) {
|
---|
| 249 | ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | screenbuffer_clear(&(connections[consnum].screenbuffer));
|
---|
| 253 |
|
---|
[eaf34f7] | 254 | break;
|
---|
[ad123964] | 255 | case CONSOLE_GOTO:
|
---|
[3993b3d] | 256 |
|
---|
| 257 | screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
---|
| 258 |
|
---|
[ad123964] | 259 | break;
|
---|
| 260 |
|
---|
[eaf34f7] | 261 | case CONSOLE_GETCHAR:
|
---|
[cf28036c] | 262 | if (keybuffer_empty(&(connections[consnum].keybuffer))) {
|
---|
| 263 | /* buffer is empty -> store request */
|
---|
| 264 | if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {
|
---|
| 265 | fifo_push(connections[consnum].keyrequests, callid);
|
---|
| 266 | connections[consnum].keyrequest_counter++;
|
---|
| 267 | } else {
|
---|
| 268 | /* no key available and too many requests => fail */
|
---|
| 269 | ipc_answer_fast(callid, ELIMIT, 0, 0);
|
---|
| 270 | }
|
---|
| 271 | continue;
|
---|
[eaf34f7] | 272 | };
|
---|
[ad123964] | 273 | keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
|
---|
[b27a97bb] | 274 |
|
---|
[eaf34f7] | 275 | break;
|
---|
| 276 | }
|
---|
[b27a97bb] | 277 | ipc_answer_fast(callid, 0, arg1, 0);
|
---|
[eaf34f7] | 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | int main(int argc, char *argv[])
|
---|
| 282 | {
|
---|
| 283 | ipcarg_t phonehash;
|
---|
[79460ae] | 284 | int kbd_phone, fb_phone;
|
---|
[51c1b003] | 285 | ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
|
---|
[79460ae] | 286 | int i;
|
---|
[da0c91e7] | 287 |
|
---|
| 288 | async_set_client_connection(client_connection);
|
---|
[51c1b003] | 289 |
|
---|
| 290 | /* Connect to keyboard driver */
|
---|
| 291 |
|
---|
[79460ae] | 292 | while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
|
---|
[eaf34f7] | 293 | usleep(10000);
|
---|
[51c1b003] | 294 | };
|
---|
| 295 |
|
---|
[eaf34f7] | 296 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
|
---|
[51c1b003] | 297 | return -1;
|
---|
| 298 | };
|
---|
[eaf34f7] | 299 | async_new_connection(phonehash, 0, NULL, keyboard_events);
|
---|
[51c1b003] | 300 |
|
---|
| 301 | /* Connect to framebuffer driver */
|
---|
| 302 |
|
---|
[3993b3d] | 303 | while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
|
---|
| 304 | usleep(10000);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
|
---|
[c1d2c9d] | 308 | ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
|
---|
[3993b3d] | 309 |
|
---|
| 310 | /* Init virtual consoles */
|
---|
[79460ae] | 311 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
---|
| 312 | connections[i].used = 0;
|
---|
| 313 | keybuffer_init(&(connections[i].keybuffer));
|
---|
[cf28036c] | 314 |
|
---|
| 315 | connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
|
---|
| 316 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
|
---|
| 317 | connections[i].keyrequest_counter = 0;
|
---|
[3993b3d] | 318 |
|
---|
| 319 | if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
|
---|
| 320 | /*FIXME: handle error */
|
---|
| 321 | return -1;
|
---|
| 322 | }
|
---|
[79460ae] | 323 | }
|
---|
[51c1b003] | 324 |
|
---|
[bd929cfb] | 325 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
|
---|
[10569b1] | 326 |
|
---|
[eaf34f7] | 327 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
|
---|
[51c1b003] | 328 | return -1;
|
---|
| 329 | };
|
---|
| 330 |
|
---|
[eaf34f7] | 331 | async_manager();
|
---|
[51c1b003] | 332 |
|
---|
| 333 | return 0;
|
---|
| 334 | }
|
---|