source: mainline/console/console.c@ 0c6984e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c6984e was 0c6984e, checked in by Josef Cejka <malyzelenyhnus@…>, 19 years ago

Console support for flush.

  • Property mode set to 100644
File size: 10.4 KB
RevLine 
[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>
[390a678]42#include <sys/mman.h>
[79460ae]43
[e1c4849]44#include "gcons.h"
45
[cf28036c]46#define MAX_KEYREQUESTS_BUFFERED 32
[51c1b003]47
48#define NAME "CONSOLE"
49
[390a678]50int active_console = 0;
[eaf34f7]51
[3993b3d]52struct {
53 int phone; /**< Framebuffer phone */
[bb51e9a8]54 ipcarg_t rows; /**< Framebuffer rows */
55 ipcarg_t cols; /**< Framebuffer columns */
[3993b3d]56} fb_info;
57
[79460ae]58typedef struct {
59 keybuffer_t keybuffer;
[cf28036c]60 FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
61 int keyrequest_counter;
[79460ae]62 int client_phone;
63 int used;
[3993b3d]64 screenbuffer_t screenbuffer;
[79460ae]65} connection_t;
66
[bb51e9a8]67
68
[79460ae]69connection_t connections[CONSOLE_COUNT];
[390a678]70keyfield_t *interbuffer = NULL;
71
[79460ae]72static int find_free_connection()
73{
74 int i = 0;
75
76 while (i < CONSOLE_COUNT) {
77 if (connections[i].used == 0)
78 return i;
79 ++i;
80 }
81 return CONSOLE_COUNT;
82}
83
84
85static int find_connection(int client_phone)
86{
87 int i = 0;
88
89 while (i < CONSOLE_COUNT) {
90 if (connections[i].client_phone == client_phone)
91 return i;
92 ++i;
93 }
94 return CONSOLE_COUNT;
95}
96
[10569b1]97/** Check key and process special keys.
98 *
99 * */
100static void write_char(int console, char key)
101{
102 screenbuffer_t *scr = &(connections[console].screenbuffer);
103
104 switch (key) {
105 case '\n':
106 scr->position_y += 1;
107 scr->position_x = 0;
108 break;
109 case '\r':
110 break;
111 case '\t':
112 scr->position_x += 8;
113 scr->position_x -= scr->position_x % 8;
114 break;
115 case '\b':
116 if (scr->position_x == 0)
117 break;
118
119 scr->position_x--;
120
121 if (console == active_console) {
[b1f51f0]122 nsend_call_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x);
[10569b1]123 }
124
125 screenbuffer_putchar(scr, ' ');
126
127 break;
128 default:
129 if (console == active_console) {
[b1f51f0]130 nsend_call_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x);
[10569b1]131 }
132
133 screenbuffer_putchar(scr, key);
134 scr->position_x++;
135 }
136
137 scr->position_y += (scr->position_x >= scr->size_x);
138
139 if (scr->position_y >= scr->size_y) {
140 scr->position_y = scr->size_y - 1;
141 screenbuffer_clear_line(scr, scr->top_line++);
[b1f51f0]142 if (console == active_console)
143 nsend_call(fb_info.phone, FB_SCROLL, 1);
[10569b1]144 }
145
146 scr->position_x = scr->position_x % scr->size_x;
[bd929cfb]147
148 if (console == active_console)
[b1f51f0]149 send_call_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x);
[10569b1]150
151}
152
153
[eaf34f7]154/* Handler for keyboard */
155static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
[51c1b003]156{
[eaf34f7]157 ipc_callid_t callid;
[51c1b003]158 ipc_call_t call;
[eaf34f7]159 int retval;
[3993b3d]160 int i, j;
[c1d2c9d]161 char c,d;
162 connection_t *conn;
[bb51e9a8]163
[eaf34f7]164 /* Ignore parameters, the connection is alread opened */
165 while (1) {
166 callid = async_get_call(&call);
167 switch (IPC_GET_METHOD(call)) {
168 case IPC_M_PHONE_HUNGUP:
169 ipc_answer_fast(callid,0,0,0);
170 /* TODO: Handle hangup */
171 return;
172 case KBD_PUSHCHAR:
173 /* got key from keyboard driver */
[b27a97bb]174
[eaf34f7]175 retval = 0;
[a805c24]176 c = IPC_GET_ARG1(call);
[eaf34f7]177 /* switch to another virtual console */
[cf28036c]178
[c1d2c9d]179 conn = &connections[active_console];
[da0c91e7]180// if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
[a805c24]181 if ((c >= '0') && (c < '0' + CONSOLE_COUNT)) {
182 if (c == '0') {
[a116ef22]183 /* switch to kernel console*/
[0c6984e]184 nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 0);
[a805c24]185 nsend_call_2(fb_info.phone, FB_SET_STYLE, DEFAULT_FOREGROUND_COLOR, DEFAULT_BACKGROUND_COLOR);
186 nsend_call(fb_info.phone, FB_CLEAR, 0);
187 /* FIXME: restore kernel console */
[a116ef22]188 __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
[a805c24]189 break;
[a116ef22]190
191 } else {
[a805c24]192 c = c - '1';
193 if (c == active_console)
194 break;
[a116ef22]195 active_console = c;
[e1c4849]196 gcons_change_console(c);
197
[a116ef22]198 }
199
[c1d2c9d]200 conn = &connections[active_console];
201
[b1f51f0]202 nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 0);
[390a678]203
204 if (interbuffer) {
[bc54f56e]205 for (i = 0; i < conn->screenbuffer.size_x; i++)
206 for (j = 0; j < conn->screenbuffer.size_y; j++)
207 interbuffer[i + j*conn->screenbuffer.size_x] = *get_field_at(&(conn->screenbuffer),i, j);
208
[a116ef22]209 sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);
[390a678]210 } else {
[b1f51f0]211 nsend_call(fb_info.phone, FB_CLEAR, 0);
[390a678]212
213
214 for (i = 0; i < conn->screenbuffer.size_x; i++)
215 for (j = 0; j < conn->screenbuffer.size_y; j++) {
216 d = get_field_at(&(conn->screenbuffer),i, j)->character;
217 if (d && d != ' ')
[b1f51f0]218 nsend_call_3(fb_info.phone, FB_PUTCHAR, d, j, i);
[390a678]219 }
220
221 }
[b1f51f0]222 nsend_call_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x);
223 nsend_call_2(fb_info.phone, FB_SET_STYLE, conn->screenbuffer.style.fg_color, \
224 conn->screenbuffer.style.bg_color);
225 send_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
[3993b3d]226
[eaf34f7]227 break;
228 }
[cf28036c]229
230 /* if client is awaiting key, send it */
[c1d2c9d]231 if (conn->keyrequest_counter > 0) {
232 conn->keyrequest_counter--;
233 ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
[cf28036c]234 break;
235 }
236
237 /*FIXME: else store key to its buffer */
[c1d2c9d]238 keybuffer_push(&conn->keybuffer, c);
[ad123964]239
[eaf34f7]240 break;
241 default:
[1029d3d3]242 retval = ENOENT;
[eaf34f7]243 }
[1029d3d3]244 ipc_answer_fast(callid, retval, 0, 0);
[eaf34f7]245 }
246}
247
248/** Default thread for new connections */
[b1f51f0]249static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
[eaf34f7]250{
[51c1b003]251 ipc_callid_t callid;
[eaf34f7]252 ipc_call_t call;
253 int consnum;
[3756912]254 ipcarg_t arg1, arg2;
[eaf34f7]255
256 if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
257 ipc_answer_fast(iid,ELIMIT,0,0);
258 return;
259 }
[10569b1]260
[eaf34f7]261 connections[consnum].used = 1;
262 connections[consnum].client_phone = IPC_GET_ARG3(call);
[3993b3d]263 screenbuffer_clear(&(connections[consnum].screenbuffer));
[10569b1]264
[eaf34f7]265 /* Accept the connection */
266 ipc_answer_fast(iid,0,0,0);
267
268 while (1) {
269 callid = async_get_call(&call);
[3756912]270 arg1 = arg2 = 0;
[eaf34f7]271 switch (IPC_GET_METHOD(call)) {
272 case IPC_M_PHONE_HUNGUP:
273 /* TODO */
274 ipc_answer_fast(callid, 0,0,0);
275 return;
276 case CONSOLE_PUTCHAR:
[10569b1]277 write_char(consnum, IPC_GET_ARG1(call));
[ad123964]278 break;
279 case CONSOLE_CLEAR:
[3993b3d]280 /* Send message to fb */
281 if (consnum == active_console) {
[b1f51f0]282 send_call(fb_info.phone, FB_CLEAR, 0);
[3993b3d]283 }
284
285 screenbuffer_clear(&(connections[consnum].screenbuffer));
286
[eaf34f7]287 break;
[ad123964]288 case CONSOLE_GOTO:
[3993b3d]289
290 screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
291
[ad123964]292 break;
[0c6984e]293
[3756912]294 case CONSOLE_GETSIZE:
295 arg1 = fb_info.cols;
296 arg2 = fb_info.rows;
297 break;
[0c6984e]298 case CONSOLE_FLUSH:
299 sync_send_2(fb_info.phone, FB_FLUSH, 0, 0, NULL, NULL);
300 break;
[eaf34f7]301 case CONSOLE_GETCHAR:
[cf28036c]302 if (keybuffer_empty(&(connections[consnum].keybuffer))) {
303 /* buffer is empty -> store request */
304 if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {
305 fifo_push(connections[consnum].keyrequests, callid);
306 connections[consnum].keyrequest_counter++;
307 } else {
308 /* no key available and too many requests => fail */
309 ipc_answer_fast(callid, ELIMIT, 0, 0);
310 }
311 continue;
[eaf34f7]312 };
[ad123964]313 keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
[b27a97bb]314
[eaf34f7]315 break;
316 }
[3756912]317 ipc_answer_fast(callid, 0, arg1, arg2);
[eaf34f7]318 }
319}
320
321int main(int argc, char *argv[])
322{
323 ipcarg_t phonehash;
[79460ae]324 int kbd_phone, fb_phone;
[51c1b003]325 ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
[79460ae]326 int i;
[da0c91e7]327
328 async_set_client_connection(client_connection);
[51c1b003]329
330 /* Connect to keyboard driver */
331
[79460ae]332 while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
[eaf34f7]333 usleep(10000);
[51c1b003]334 };
335
[eaf34f7]336 if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
[51c1b003]337 return -1;
338 };
339
340 /* Connect to framebuffer driver */
341
[3993b3d]342 while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
343 usleep(10000);
344 }
[e1c4849]345
346 /* Initialize gcons */
347 gcons_init(fb_info.phone);
348 /* Synchronize, the gcons can have something in queue */
349 sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
350
[3993b3d]351
352 ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
[b1f51f0]353 nsend_call_2(fb_info.phone, FB_SET_STYLE, DEFAULT_FOREGROUND_COLOR, DEFAULT_BACKGROUND_COLOR);
[a805c24]354 nsend_call(fb_info.phone, FB_CLEAR, 0);
[3993b3d]355
356 /* Init virtual consoles */
[79460ae]357 for (i = 0; i < CONSOLE_COUNT; i++) {
358 connections[i].used = 0;
359 keybuffer_init(&(connections[i].keybuffer));
[cf28036c]360
361 connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
362 connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
363 connections[i].keyrequest_counter = 0;
[3993b3d]364
365 if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
366 /*FIXME: handle error */
367 return -1;
368 }
[79460ae]369 }
[51c1b003]370
[390a678]371 if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
372 if (ipc_call_sync_3(fb_info.phone, IPC_M_AS_AREA_SEND, (ipcarg_t)interbuffer, 0, AS_AREA_READ | AS_AREA_CACHEABLE, NULL, NULL, NULL) != 0) {
373 munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
374 interbuffer = NULL;
375 }
376 }
[a805c24]377
378 /* FIXME: save kernel console screen */
[390a678]379
[bb51e9a8]380 async_new_connection(phonehash, 0, NULL, keyboard_events);
381
[e1c4849]382 sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
[a805c24]383 nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
[10569b1]384
[b1f51f0]385 /* Register at NS */
[eaf34f7]386 if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
[51c1b003]387 return -1;
388 };
389
[eaf34f7]390 async_manager();
[51c1b003]391
392 return 0;
393}
[a805c24]394
Note: See TracBrowser for help on using the repository browser.