Subversion Repositories HelenOS

Rev

Rev 1521 | Rev 1523 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1445 cejka 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>
1451 cejka 31
#include <fb.h>
1445 cejka 32
#include <ipc/ipc.h>
1451 cejka 33
#include <ipc/fb.h>
1445 cejka 34
#include <ipc/services.h>
35
#include <errno.h>
1451 cejka 36
#include <key_buffer.h>
37
#include <console.h>
1453 palkovsky 38
#include <unistd.h>
39
#include <async.h>
1481 cejka 40
#include <libadt/fifo.h>
1487 cejka 41
#include <screenbuffer.h>
1512 cejka 42
#include <sys/mman.h>
1445 cejka 43
 
1522 palkovsky 44
#include "gcons.h"
45
 
1481 cejka 46
#define MAX_KEYREQUESTS_BUFFERED 32
1451 cejka 47
 
1445 cejka 48
#define NAME "CONSOLE"
49
 
1512 cejka 50
int active_console = 0;
1453 palkovsky 51
 
1487 cejka 52
struct {
53
	int phone;		/**< Framebuffer phone */
1506 cejka 54
	ipcarg_t rows;		/**< Framebuffer rows */
55
	ipcarg_t cols;		/**< Framebuffer columns */
1487 cejka 56
} fb_info;
57
 
1451 cejka 58
typedef struct {
59
	keybuffer_t keybuffer;
1481 cejka 60
	FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
61
	int keyrequest_counter;
1451 cejka 62
	int client_phone;
63
	int used;
1487 cejka 64
	screenbuffer_t screenbuffer;
1451 cejka 65
} connection_t;
66
 
1506 cejka 67
 
68
 
1451 cejka 69
connection_t connections[CONSOLE_COUNT];
1512 cejka 70
keyfield_t *interbuffer = NULL;
71
 
1451 cejka 72
static 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
 
85
static 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
 
1497 cejka 97
/** Check key and process special keys. 
98
 *
99
 * */
100
static 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) {
1518 palkovsky 122
				nsend_call_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x);
1497 cejka 123
			}
124
 
125
			screenbuffer_putchar(scr, ' ');
126
 
127
			break;
128
		default:	
129
			if (console == active_console) {
1518 palkovsky 130
				nsend_call_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x);
1497 cejka 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++);
1518 palkovsky 142
		if (console == active_console)
143
			nsend_call(fb_info.phone, FB_SCROLL, 1);
1497 cejka 144
	}
145
 
146
	scr->position_x = scr->position_x % scr->size_x;
147
 
1504 cejka 148
	if (console == active_console)	
1518 palkovsky 149
		send_call_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x);
1504 cejka 150
 
1497 cejka 151
}
152
 
153
 
1453 palkovsky 154
/* Handler for keyboard */
155
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 156
{
1453 palkovsky 157
	ipc_callid_t callid;
1445 cejka 158
	ipc_call_t call;
1453 palkovsky 159
	int retval;
1487 cejka 160
	int i, j;
1489 palkovsky 161
	char c,d;
162
	connection_t *conn;
1506 cejka 163
 
1453 palkovsky 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 */
1465 cejka 174
 
1453 palkovsky 175
			retval = 0;
1520 cejka 176
			c = IPC_GET_ARG1(call);
1453 palkovsky 177
			/* switch to another virtual console */
1481 cejka 178
 
1489 palkovsky 179
			conn = &connections[active_console];
1490 palkovsky 180
//			if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
1520 cejka 181
			if ((c >= '0') && (c < '0' + CONSOLE_COUNT)) {
182
				if (c == '0') {
1517 cejka 183
					/* switch to kernel console*/
1520 cejka 184
					sync_send_2(fb_info.phone, FB_CURSOR_VISIBILITY, 0, 0, NULL, NULL);
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 */
1517 cejka 188
					 __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
1520 cejka 189
					 break;
1517 cejka 190
 
191
				} else {
1520 cejka 192
					c = c - '1';
193
					if (c == active_console)
194
						break;
1517 cejka 195
					active_console = c;
1522 palkovsky 196
					gcons_change_console(c);
197
 
1517 cejka 198
				}
199
 
1489 palkovsky 200
				conn = &connections[active_console];
201
 
1518 palkovsky 202
				nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 0); 
1512 cejka 203
 
204
				if (interbuffer) {
1516 cejka 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
 
1517 cejka 209
					sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);		
1512 cejka 210
				} else {
1518 palkovsky 211
					nsend_call(fb_info.phone, FB_CLEAR, 0);
1497 cejka 212
 
1512 cejka 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 != ' ')
1518 palkovsky 218
								nsend_call_3(fb_info.phone, FB_PUTCHAR, d, j, i);
1512 cejka 219
						}
1499 cejka 220
 
1512 cejka 221
				}
1518 palkovsky 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); 
1487 cejka 226
 
1453 palkovsky 227
				break;
228
			}
1481 cejka 229
 
230
			/* if client is awaiting key, send it */
1489 palkovsky 231
			if (conn->keyrequest_counter > 0) {		
232
				conn->keyrequest_counter--;
233
				ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
1481 cejka 234
				break;
235
			}
236
 
237
			/*FIXME: else store key to its buffer */
1489 palkovsky 238
			keybuffer_push(&conn->keybuffer, c);
1476 cejka 239
 
1453 palkovsky 240
			break;
241
		default:
1459 palkovsky 242
			retval = ENOENT;
1453 palkovsky 243
		}		
1459 palkovsky 244
		ipc_answer_fast(callid, retval, 0, 0);
1453 palkovsky 245
	}
246
}
247
 
248
/** Default thread for new connections */
1518 palkovsky 249
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1453 palkovsky 250
{
1445 cejka 251
	ipc_callid_t callid;
1453 palkovsky 252
	ipc_call_t call;
253
	int consnum;
1521 cejka 254
	ipcarg_t arg1, arg2;
1453 palkovsky 255
 
256
	if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
257
		ipc_answer_fast(iid,ELIMIT,0,0);
258
		return;
259
	}
1497 cejka 260
 
1453 palkovsky 261
	connections[consnum].used = 1;
262
	connections[consnum].client_phone = IPC_GET_ARG3(call);
1487 cejka 263
	screenbuffer_clear(&(connections[consnum].screenbuffer));
1497 cejka 264
 
1453 palkovsky 265
	/* Accept the connection */
266
	ipc_answer_fast(iid,0,0,0);
267
 
268
	while (1) {
269
		callid = async_get_call(&call);
1521 cejka 270
		arg1 = arg2 = 0;
1453 palkovsky 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:
1497 cejka 277
			write_char(consnum, IPC_GET_ARG1(call));
1453 palkovsky 278
			break;
1476 cejka 279
		case CONSOLE_CLEAR:
1487 cejka 280
			/* Send message to fb */
281
			if (consnum == active_console) {
1518 palkovsky 282
				send_call(fb_info.phone, FB_CLEAR, 0); 
1487 cejka 283
			}
284
 
285
			screenbuffer_clear(&(connections[consnum].screenbuffer));
286
 
1476 cejka 287
			break;
288
		case CONSOLE_GOTO:
1487 cejka 289
 
290
			screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
291
 
1476 cejka 292
			break;
1521 cejka 293
		case CONSOLE_GETSIZE:
294
			arg1 = fb_info.cols;
295
			arg2 = fb_info.rows;
296
			break;
1476 cejka 297
 
1453 palkovsky 298
		case CONSOLE_GETCHAR:
1481 cejka 299
			if (keybuffer_empty(&(connections[consnum].keybuffer))) {
300
				/* buffer is empty -> store request */
301
				if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {		
302
					fifo_push(connections[consnum].keyrequests, callid);
303
					connections[consnum].keyrequest_counter++;
304
				} else {
305
					/* no key available and too many requests => fail */
306
					ipc_answer_fast(callid, ELIMIT, 0, 0);
307
				}
308
				continue;
1453 palkovsky 309
			};
1476 cejka 310
			keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
1465 cejka 311
 
1453 palkovsky 312
			break;
313
		}
1521 cejka 314
		ipc_answer_fast(callid, 0, arg1, arg2);
1453 palkovsky 315
	}
316
}
317
 
318
int main(int argc, char *argv[])
319
{
320
	ipcarg_t phonehash;
1451 cejka 321
	int kbd_phone, fb_phone;
1445 cejka 322
	ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
1451 cejka 323
	int i;
1490 palkovsky 324
 
325
	async_set_client_connection(client_connection);
1445 cejka 326
 
327
	/* Connect to keyboard driver */
328
 
1451 cejka 329
	while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
1453 palkovsky 330
		usleep(10000);
1445 cejka 331
	};
332
 
1453 palkovsky 333
	if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 334
		return -1;
335
	};
336
 
337
	/* Connect to framebuffer driver */
338
 
1487 cejka 339
	while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
340
		usleep(10000);
341
	}
1522 palkovsky 342
 
343
	/* Initialize gcons */
344
	gcons_init(fb_info.phone);
345
	/* Synchronize, the gcons can have something in queue */
346
	sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
347
 
1487 cejka 348
 
349
	ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols)); 
1518 palkovsky 350
	nsend_call_2(fb_info.phone, FB_SET_STYLE, DEFAULT_FOREGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); 
1520 cejka 351
	nsend_call(fb_info.phone, FB_CLEAR, 0); 
1487 cejka 352
 
353
	/* Init virtual consoles */
1451 cejka 354
	for (i = 0; i < CONSOLE_COUNT; i++) {
355
		connections[i].used = 0;
356
		keybuffer_init(&(connections[i].keybuffer));
1481 cejka 357
 
358
		connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
359
		connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
360
		connections[i].keyrequest_counter = 0;
1487 cejka 361
 
362
		if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
363
			/*FIXME: handle error */
364
			return -1;
365
		}
1451 cejka 366
	}
1445 cejka 367
 
1512 cejka 368
	if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
369
		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) {
370
			munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
371
			interbuffer = NULL;
372
		}
373
	}
1520 cejka 374
 
375
	/* FIXME: save kernel console screen */
1512 cejka 376
 
1506 cejka 377
	async_new_connection(phonehash, 0, NULL, keyboard_events);
378
 
1522 palkovsky 379
	sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL); 
1520 cejka 380
	nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1); 
1497 cejka 381
 
1518 palkovsky 382
	/* Register at NS */
1453 palkovsky 383
	if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 384
		return -1;
385
	};
386
 
1453 palkovsky 387
	async_manager();
1451 cejka 388
 
1445 cejka 389
	return 0;	
390
}
1520 cejka 391