[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 */
|
---|
[bb51e9a8] | 52 | ipcarg_t rows; /**< Framebuffer rows */
|
---|
| 53 | ipcarg_t cols; /**< Framebuffer columns */
|
---|
[3993b3d] | 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 |
|
---|
[bb51e9a8] | 65 |
|
---|
| 66 |
|
---|
[79460ae] | 67 | connection_t connections[CONSOLE_COUNT];
|
---|
| 68 |
|
---|
| 69 | static int find_free_connection()
|
---|
| 70 | {
|
---|
| 71 | int i = 0;
|
---|
| 72 |
|
---|
| 73 | while (i < CONSOLE_COUNT) {
|
---|
| 74 | if (connections[i].used == 0)
|
---|
| 75 | return i;
|
---|
| 76 | ++i;
|
---|
| 77 | }
|
---|
| 78 | return CONSOLE_COUNT;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | static int find_connection(int client_phone)
|
---|
| 83 | {
|
---|
| 84 | int i = 0;
|
---|
| 85 |
|
---|
| 86 | while (i < CONSOLE_COUNT) {
|
---|
| 87 | if (connections[i].client_phone == client_phone)
|
---|
| 88 | return i;
|
---|
| 89 | ++i;
|
---|
| 90 | }
|
---|
| 91 | return CONSOLE_COUNT;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[10569b1] | 94 | /** Check key and process special keys.
|
---|
| 95 | *
|
---|
| 96 | * */
|
---|
| 97 | static void write_char(int console, char key)
|
---|
| 98 | {
|
---|
| 99 | screenbuffer_t *scr = &(connections[console].screenbuffer);
|
---|
| 100 |
|
---|
| 101 | switch (key) {
|
---|
| 102 | case '\n':
|
---|
| 103 | scr->position_y += 1;
|
---|
| 104 | scr->position_x = 0;
|
---|
| 105 | break;
|
---|
| 106 | case '\r':
|
---|
| 107 | break;
|
---|
| 108 | case '\t':
|
---|
| 109 | scr->position_x += 8;
|
---|
| 110 | scr->position_x -= scr->position_x % 8;
|
---|
| 111 | break;
|
---|
| 112 | case '\b':
|
---|
| 113 | if (scr->position_x == 0)
|
---|
| 114 | break;
|
---|
| 115 |
|
---|
| 116 | scr->position_x--;
|
---|
| 117 |
|
---|
| 118 | if (console == active_console) {
|
---|
| 119 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x, NULL, NULL);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | screenbuffer_putchar(scr, ' ');
|
---|
| 123 |
|
---|
| 124 | break;
|
---|
| 125 | default:
|
---|
| 126 | if (console == active_console) {
|
---|
| 127 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x, NULL, NULL);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | screenbuffer_putchar(scr, key);
|
---|
| 131 | scr->position_x++;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | scr->position_y += (scr->position_x >= scr->size_x);
|
---|
| 135 |
|
---|
| 136 | if (scr->position_y >= scr->size_y) {
|
---|
| 137 | scr->position_y = scr->size_y - 1;
|
---|
| 138 | screenbuffer_clear_line(scr, scr->top_line++);
|
---|
| 139 | ipc_call_async(fb_info.phone, FB_SCROLL, 1, NULL, NULL);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | scr->position_x = scr->position_x % scr->size_x;
|
---|
[bd929cfb] | 143 |
|
---|
| 144 | if (console == active_console)
|
---|
| 145 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x, NULL, NULL);
|
---|
[10569b1] | 146 |
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
[eaf34f7] | 150 | /* Handler for keyboard */
|
---|
| 151 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[51c1b003] | 152 | {
|
---|
[eaf34f7] | 153 | ipc_callid_t callid;
|
---|
[51c1b003] | 154 | ipc_call_t call;
|
---|
[eaf34f7] | 155 | int retval;
|
---|
[3993b3d] | 156 | int i, j;
|
---|
[c1d2c9d] | 157 | char c,d;
|
---|
| 158 | connection_t *conn;
|
---|
[bb51e9a8] | 159 | keyfield_t *interbuffer = NULL;
|
---|
[eaf34f7] | 160 |
|
---|
[bb51e9a8] | 161 | // interbuffer = mmap(,, PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS, , );
|
---|
| 162 |
|
---|
[eaf34f7] | 163 | /* Ignore parameters, the connection is alread opened */
|
---|
| 164 | while (1) {
|
---|
| 165 | callid = async_get_call(&call);
|
---|
| 166 | switch (IPC_GET_METHOD(call)) {
|
---|
| 167 | case IPC_M_PHONE_HUNGUP:
|
---|
| 168 | ipc_answer_fast(callid,0,0,0);
|
---|
| 169 | /* TODO: Handle hangup */
|
---|
| 170 | return;
|
---|
| 171 | case KBD_PUSHCHAR:
|
---|
| 172 | /* got key from keyboard driver */
|
---|
[b27a97bb] | 173 |
|
---|
[eaf34f7] | 174 | retval = 0;
|
---|
[ad123964] | 175 | c = IPC_GET_ARG1(call);
|
---|
[eaf34f7] | 176 | /* switch to another virtual console */
|
---|
[cf28036c] | 177 |
|
---|
[c1d2c9d] | 178 | conn = &connections[active_console];
|
---|
[da0c91e7] | 179 | // if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
|
---|
| 180 | if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
|
---|
[3993b3d] | 181 | /*FIXME: draw another console content from buffer */
|
---|
[838c48e] | 182 | if (c - '1' == active_console)
|
---|
[10569b1] | 183 | break;
|
---|
[da0c91e7] | 184 | active_console = c - '1';
|
---|
[c1d2c9d] | 185 | conn = &connections[active_console];
|
---|
| 186 |
|
---|
| 187 | ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
|
---|
[3993b3d] | 188 | ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
|
---|
[10569b1] | 189 |
|
---|
[c1d2c9d] | 190 | for (i = 0; i < conn->screenbuffer.size_x; i++)
|
---|
| 191 | for (j = 0; j < conn->screenbuffer.size_y; j++) {
|
---|
| 192 | d = get_field_at(&(conn->screenbuffer),i, j)->character;
|
---|
| 193 | if (d && d != ' ')
|
---|
| 194 | ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
|
---|
| 195 | }
|
---|
[838c48e] | 196 |
|
---|
[c1d2c9d] | 197 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
|
---|
| 198 | ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
|
---|
[3993b3d] | 199 |
|
---|
[eaf34f7] | 200 | break;
|
---|
| 201 | }
|
---|
[cf28036c] | 202 |
|
---|
| 203 | /* if client is awaiting key, send it */
|
---|
[c1d2c9d] | 204 | if (conn->keyrequest_counter > 0) {
|
---|
| 205 | conn->keyrequest_counter--;
|
---|
| 206 | ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
|
---|
[cf28036c] | 207 | break;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*FIXME: else store key to its buffer */
|
---|
[c1d2c9d] | 211 | keybuffer_push(&conn->keybuffer, c);
|
---|
[ad123964] | 212 |
|
---|
[eaf34f7] | 213 | break;
|
---|
| 214 | default:
|
---|
[1029d3d3] | 215 | retval = ENOENT;
|
---|
[eaf34f7] | 216 | }
|
---|
[1029d3d3] | 217 | ipc_answer_fast(callid, retval, 0, 0);
|
---|
[eaf34f7] | 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /** Default thread for new connections */
|
---|
| 222 | void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 223 | {
|
---|
[51c1b003] | 224 | ipc_callid_t callid;
|
---|
[eaf34f7] | 225 | ipc_call_t call;
|
---|
| 226 | int consnum;
|
---|
| 227 | ipcarg_t arg1;
|
---|
| 228 |
|
---|
| 229 | if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
|
---|
| 230 | ipc_answer_fast(iid,ELIMIT,0,0);
|
---|
| 231 | return;
|
---|
| 232 | }
|
---|
[10569b1] | 233 |
|
---|
[eaf34f7] | 234 | connections[consnum].used = 1;
|
---|
| 235 | connections[consnum].client_phone = IPC_GET_ARG3(call);
|
---|
[3993b3d] | 236 | screenbuffer_clear(&(connections[consnum].screenbuffer));
|
---|
[10569b1] | 237 |
|
---|
[eaf34f7] | 238 | /* Accept the connection */
|
---|
| 239 | ipc_answer_fast(iid,0,0,0);
|
---|
| 240 |
|
---|
| 241 | while (1) {
|
---|
| 242 | callid = async_get_call(&call);
|
---|
| 243 | switch (IPC_GET_METHOD(call)) {
|
---|
| 244 | case IPC_M_PHONE_HUNGUP:
|
---|
| 245 | /* TODO */
|
---|
| 246 | ipc_answer_fast(callid, 0,0,0);
|
---|
| 247 | return;
|
---|
| 248 | case CONSOLE_PUTCHAR:
|
---|
[10569b1] | 249 | write_char(consnum, IPC_GET_ARG1(call));
|
---|
[ad123964] | 250 | break;
|
---|
| 251 | case CONSOLE_CLEAR:
|
---|
[3993b3d] | 252 | /* Send message to fb */
|
---|
| 253 | if (consnum == active_console) {
|
---|
| 254 | ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | screenbuffer_clear(&(connections[consnum].screenbuffer));
|
---|
| 258 |
|
---|
[eaf34f7] | 259 | break;
|
---|
[ad123964] | 260 | case CONSOLE_GOTO:
|
---|
[3993b3d] | 261 |
|
---|
| 262 | screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
---|
| 263 |
|
---|
[ad123964] | 264 | break;
|
---|
| 265 |
|
---|
[eaf34f7] | 266 | case CONSOLE_GETCHAR:
|
---|
[cf28036c] | 267 | if (keybuffer_empty(&(connections[consnum].keybuffer))) {
|
---|
| 268 | /* buffer is empty -> store request */
|
---|
| 269 | if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {
|
---|
| 270 | fifo_push(connections[consnum].keyrequests, callid);
|
---|
| 271 | connections[consnum].keyrequest_counter++;
|
---|
| 272 | } else {
|
---|
| 273 | /* no key available and too many requests => fail */
|
---|
| 274 | ipc_answer_fast(callid, ELIMIT, 0, 0);
|
---|
| 275 | }
|
---|
| 276 | continue;
|
---|
[eaf34f7] | 277 | };
|
---|
[ad123964] | 278 | keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
|
---|
[b27a97bb] | 279 |
|
---|
[eaf34f7] | 280 | break;
|
---|
| 281 | }
|
---|
[b27a97bb] | 282 | ipc_answer_fast(callid, 0, arg1, 0);
|
---|
[eaf34f7] | 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | int main(int argc, char *argv[])
|
---|
| 287 | {
|
---|
| 288 | ipcarg_t phonehash;
|
---|
[79460ae] | 289 | int kbd_phone, fb_phone;
|
---|
[51c1b003] | 290 | ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
|
---|
[79460ae] | 291 | int i;
|
---|
[da0c91e7] | 292 |
|
---|
| 293 | async_set_client_connection(client_connection);
|
---|
[51c1b003] | 294 |
|
---|
| 295 | /* Connect to keyboard driver */
|
---|
| 296 |
|
---|
[79460ae] | 297 | while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
|
---|
[eaf34f7] | 298 | usleep(10000);
|
---|
[51c1b003] | 299 | };
|
---|
| 300 |
|
---|
[eaf34f7] | 301 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
|
---|
[51c1b003] | 302 | return -1;
|
---|
| 303 | };
|
---|
| 304 |
|
---|
| 305 | /* Connect to framebuffer driver */
|
---|
| 306 |
|
---|
[3993b3d] | 307 | while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
|
---|
| 308 | usleep(10000);
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
|
---|
[c1d2c9d] | 312 | ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
|
---|
[3993b3d] | 313 |
|
---|
| 314 | /* Init virtual consoles */
|
---|
[79460ae] | 315 | for (i = 0; i < CONSOLE_COUNT; i++) {
|
---|
| 316 | connections[i].used = 0;
|
---|
| 317 | keybuffer_init(&(connections[i].keybuffer));
|
---|
[cf28036c] | 318 |
|
---|
| 319 | connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
|
---|
| 320 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
|
---|
| 321 | connections[i].keyrequest_counter = 0;
|
---|
[3993b3d] | 322 |
|
---|
| 323 | if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
|
---|
| 324 | /*FIXME: handle error */
|
---|
| 325 | return -1;
|
---|
| 326 | }
|
---|
[79460ae] | 327 | }
|
---|
[51c1b003] | 328 |
|
---|
[bb51e9a8] | 329 | async_new_connection(phonehash, 0, NULL, keyboard_events);
|
---|
| 330 |
|
---|
[bd929cfb] | 331 | ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
|
---|
[10569b1] | 332 |
|
---|
[eaf34f7] | 333 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
|
---|
[51c1b003] | 334 | return -1;
|
---|
| 335 | };
|
---|
| 336 |
|
---|
[eaf34f7] | 337 | async_manager();
|
---|
[51c1b003] | 338 |
|
---|
| 339 | return 0;
|
---|
| 340 | }
|
---|