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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0bf84cc was 59ed572, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Tetris is working :-) Milstone completed!

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