Subversion Repositories HelenOS-historic

Rev

Rev 1563 | Rev 1574 | 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
 
1526 cejka 50
/** Index of currently used virtual console.
51
 */
1512 cejka 52
int active_console = 0;
1453 palkovsky 53
 
1526 cejka 54
/** Information about framebuffer
55
 */
1487 cejka 56
struct {
57
	int phone;		/**< Framebuffer phone */
1506 cejka 58
	ipcarg_t rows;		/**< Framebuffer rows */
59
	ipcarg_t cols;		/**< Framebuffer columns */
1487 cejka 60
} fb_info;
61
 
1526 cejka 62
 
1451 cejka 63
typedef struct {
1526 cejka 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. */
1451 cejka 70
} connection_t;
71
 
1552 palkovsky 72
static connection_t connections[CONSOLE_COUNT];	/**< Array of data for virtual consoles */
73
static keyfield_t *interbuffer = NULL;			/**< Pointer to memory shared with framebufer used for faster virt. console switching */
1506 cejka 74
 
1552 palkovsky 75
static int kernel_pixmap = -1;      /**< Number of fb pixmap, where kernel console is stored */
1506 cejka 76
 
1552 palkovsky 77
 
1526 cejka 78
/** Find unused virtual console.
79
 *
80
 */
1451 cejka 81
static int find_free_connection() 
82
{
83
	int i = 0;
84
 
85
	while (i < CONSOLE_COUNT) {
86
		if (connections[i].used == 0)
87
			return i;
88
		++i;
89
	}
90
	return CONSOLE_COUNT;
91
}
92
 
1526 cejka 93
/** Find index of virtual console used by client with given phone.
94
 *
95
 */
1451 cejka 96
static int find_connection(int client_phone) 
97
{
98
	int i = 0;
99
 
100
	while (i < CONSOLE_COUNT) {
101
		if (connections[i].client_phone == client_phone)
102
			return i;
103
		++i;
104
	}
105
	return  CONSOLE_COUNT;
106
}
107
 
1552 palkovsky 108
static void clrscr(void)
109
{
110
	nsend_call(fb_info.phone, FB_CLEAR, 0);
111
}
112
 
113
static void curs_visibility(int v)
114
{
115
	send_call(fb_info.phone, FB_CURSOR_VISIBILITY, v); 
116
}
117
 
118
static void curs_goto(int row, int col)
119
{
120
	nsend_call_2(fb_info.phone, FB_CURSOR_GOTO, row, col); 
121
 
122
}
123
 
124
static void set_style(style_t *style)
125
{
126
	nsend_call_2(fb_info.phone, FB_SET_STYLE, style->fg_color, style->bg_color); 
127
}
128
 
129
static void set_style_col(int fgcolor, int bgcolor)
130
{
131
	nsend_call_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor); 
132
}
133
 
134
static void prtchr(char c, int row, int col)
135
{
136
	nsend_call_3(fb_info.phone, FB_PUTCHAR, c, row, col);
137
 
138
}
139
 
1497 cejka 140
/** Check key and process special keys. 
141
 *
142
 * */
143
static void write_char(int console, char key)
144
{
145
	screenbuffer_t *scr = &(connections[console].screenbuffer);
146
 
147
	switch (key) {
148
		case '\n':
149
			scr->position_y += 1;
150
			scr->position_x =  0;
151
			break;
152
		case '\r':
153
			break;
154
		case '\t':
155
			scr->position_x += 8;
156
			scr->position_x -= scr->position_x % 8; 
157
			break;
158
		case '\b':
159
			if (scr->position_x == 0) 
160
				break;
161
 
162
			scr->position_x--;
163
 
1552 palkovsky 164
			if (console == active_console)
165
				prtchr(' ', scr->position_y, scr->position_x);
1497 cejka 166
 
167
			screenbuffer_putchar(scr, ' ');
168
 
169
			break;
170
		default:	
1552 palkovsky 171
			if (console == active_console)
172
				prtchr(key, scr->position_y, scr->position_x);
1497 cejka 173
 
174
			screenbuffer_putchar(scr, key);
175
			scr->position_x++;
176
	}
177
 
178
	scr->position_y += (scr->position_x >= scr->size_x);
179
 
180
	if (scr->position_y >= scr->size_y) {
181
		scr->position_y = scr->size_y - 1;
182
		screenbuffer_clear_line(scr, scr->top_line++);
1518 palkovsky 183
		if (console == active_console)
184
			nsend_call(fb_info.phone, FB_SCROLL, 1);
1497 cejka 185
	}
186
 
187
	scr->position_x = scr->position_x % scr->size_x;
188
 
1552 palkovsky 189
	if (console == active_console)
190
		curs_goto(scr->position_y, scr->position_x);
1504 cejka 191
 
1497 cejka 192
}
193
 
1552 palkovsky 194
/** Save current screen to pixmap, draw old pixmap
195
 *
196
 * @param oldpixmap Old pixmap
197
 * @return ID of pixmap of current screen
198
 */
199
static int switch_screens(int oldpixmap)
200
{
201
	int newpmap;
202
 
203
	/* Save screen */
204
	newpmap = sync_send(fb_info.phone, FB_VP2PIXMAP, 0, NULL);
205
	if (newpmap < 0)
206
		return -1;
1497 cejka 207
 
1552 palkovsky 208
	if (oldpixmap != -1) {
209
		/* Show old screen */
210
		nsend_call_2(fb_info.phone, FB_VP_DRAW_PIXMAP, 0, oldpixmap);
211
		/* Drop old pixmap */
212
		nsend_call(fb_info.phone, FB_DROP_PIXMAP, oldpixmap);
213
	}
214
 
215
	return newpmap;
216
}
217
 
218
/** Switch to new console */
219
static void change_console(int newcons)
220
{
221
	connection_t *conn;
222
	static int console_pixmap = -1;
223
	int i, j;
224
	char c;
225
 
226
	if (newcons == active_console)
227
		return;
228
 
1555 palkovsky 229
	if (newcons == KERNEL_CONSOLE) {
230
		if (active_console == KERNEL_CONSOLE)
1552 palkovsky 231
			return;
1555 palkovsky 232
		active_console = KERNEL_CONSOLE;
1552 palkovsky 233
		curs_visibility(0);
234
 
235
		if (kernel_pixmap == -1) { 
236
			/* store/restore unsupported */
237
			set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
238
			clrscr();
239
		} else {
240
			gcons_in_kernel();
241
			console_pixmap = switch_screens(kernel_pixmap);
242
			kernel_pixmap = -1;
243
		}
244
 
245
		__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
246
		return;
247
	} 
248
 
249
	if (console_pixmap != -1) {
250
		kernel_pixmap = switch_screens(console_pixmap);
251
		console_pixmap = -1;
252
	}
253
 
254
	active_console = newcons;
255
	gcons_change_console(newcons);
256
	conn = &connections[active_console];
257
 
1563 palkovsky 258
	set_style(&conn->screenbuffer.style);
259
	curs_goto(conn->screenbuffer.position_y, conn->screenbuffer.position_x);
1552 palkovsky 260
	if (interbuffer) {
261
		for (i = 0; i < conn->screenbuffer.size_x; i++)
262
			for (j = 0; j < conn->screenbuffer.size_y; j++) 
263
				interbuffer[i + j*conn->screenbuffer.size_x] = *get_field_at(&(conn->screenbuffer),i, j);
1563 palkovsky 264
		/* This call can preempt, but we are already at the end */
1552 palkovsky 265
		sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);		
1567 palkovsky 266
		curs_visibility(1);
1552 palkovsky 267
	} else {
1563 palkovsky 268
		curs_visibility(0);
1552 palkovsky 269
		clrscr();
270
 
271
		for (i = 0; i < conn->screenbuffer.size_x; i++)
272
			for (j = 0; j < conn->screenbuffer.size_y; j++) {
273
				c = get_field_at(&(conn->screenbuffer),i, j)->character;
274
				if (c && c != ' ')
275
					prtchr(c, j, i);
276
			}
277
 
1563 palkovsky 278
		curs_visibility(1);
1552 palkovsky 279
	}
280
}
281
 
1526 cejka 282
/** Handler for keyboard */
1453 palkovsky 283
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 284
{
1453 palkovsky 285
	ipc_callid_t callid;
1445 cejka 286
	ipc_call_t call;
1453 palkovsky 287
	int retval;
1560 vana 288
	int c;
1489 palkovsky 289
	connection_t *conn;
1506 cejka 290
 
1453 palkovsky 291
	/* Ignore parameters, the connection is alread opened */
292
	while (1) {
293
		callid = async_get_call(&call);
294
		switch (IPC_GET_METHOD(call)) {
295
		case IPC_M_PHONE_HUNGUP:
296
			ipc_answer_fast(callid,0,0,0);
297
			/* TODO: Handle hangup */
298
			return;
299
		case KBD_PUSHCHAR:
300
			/* got key from keyboard driver */
1465 cejka 301
 
1453 palkovsky 302
			retval = 0;
1520 cejka 303
			c = IPC_GET_ARG1(call);
1453 palkovsky 304
			/* switch to another virtual console */
1481 cejka 305
 
1489 palkovsky 306
			conn = &connections[active_console];
1490 palkovsky 307
//			if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
1560 vana 308
			if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
309
				if (c == 0x112)
1555 palkovsky 310
					change_console(KERNEL_CONSOLE);
1552 palkovsky 311
				else
1560 vana 312
					change_console(c - 0x101);
1453 palkovsky 313
				break;
314
			}
1481 cejka 315
 
316
			/* if client is awaiting key, send it */
1489 palkovsky 317
			if (conn->keyrequest_counter > 0) {		
318
				conn->keyrequest_counter--;
319
				ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
1481 cejka 320
				break;
321
			}
322
 
323
			/*FIXME: else store key to its buffer */
1489 palkovsky 324
			keybuffer_push(&conn->keybuffer, c);
1476 cejka 325
 
1453 palkovsky 326
			break;
327
		default:
1459 palkovsky 328
			retval = ENOENT;
1453 palkovsky 329
		}		
1459 palkovsky 330
		ipc_answer_fast(callid, retval, 0, 0);
1453 palkovsky 331
	}
332
}
333
 
334
/** Default thread for new connections */
1518 palkovsky 335
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1453 palkovsky 336
{
1445 cejka 337
	ipc_callid_t callid;
1453 palkovsky 338
	ipc_call_t call;
339
	int consnum;
1521 cejka 340
	ipcarg_t arg1, arg2;
1453 palkovsky 341
 
342
	if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
343
		ipc_answer_fast(iid,ELIMIT,0,0);
344
		return;
345
	}
1555 palkovsky 346
 
347
	gcons_notify_connect(consnum);
1453 palkovsky 348
	connections[consnum].used = 1;
349
	connections[consnum].client_phone = IPC_GET_ARG3(call);
1487 cejka 350
	screenbuffer_clear(&(connections[consnum].screenbuffer));
1497 cejka 351
 
1453 palkovsky 352
	/* Accept the connection */
353
	ipc_answer_fast(iid,0,0,0);
354
 
355
	while (1) {
356
		callid = async_get_call(&call);
1521 cejka 357
		arg1 = arg2 = 0;
1453 palkovsky 358
		switch (IPC_GET_METHOD(call)) {
359
		case IPC_M_PHONE_HUNGUP:
360
			/* TODO */
361
			ipc_answer_fast(callid, 0,0,0);
362
			return;
363
		case CONSOLE_PUTCHAR:
1497 cejka 364
			write_char(consnum, IPC_GET_ARG1(call));
1528 palkovsky 365
			gcons_notify_char(consnum);
1453 palkovsky 366
			break;
1476 cejka 367
		case CONSOLE_CLEAR:
1487 cejka 368
			/* Send message to fb */
369
			if (consnum == active_console) {
1518 palkovsky 370
				send_call(fb_info.phone, FB_CLEAR, 0); 
1487 cejka 371
			}
372
 
373
			screenbuffer_clear(&(connections[consnum].screenbuffer));
374
 
1476 cejka 375
			break;
376
		case CONSOLE_GOTO:
1487 cejka 377
 
1528 palkovsky 378
			screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call), IPC_GET_ARG1(call));
1567 palkovsky 379
			if (consnum == active_console)
380
				curs_goto(IPC_GET_ARG1(call),IPC_GET_ARG2(call));
1487 cejka 381
 
1476 cejka 382
			break;
1523 cejka 383
 
1521 cejka 384
		case CONSOLE_GETSIZE:
1528 palkovsky 385
			arg1 = fb_info.rows;
386
			arg2 = fb_info.cols;
1521 cejka 387
			break;
1523 cejka 388
		case CONSOLE_FLUSH:
389
			sync_send_2(fb_info.phone, FB_FLUSH, 0, 0, NULL, NULL);		
390
			break;
1525 cejka 391
		case CONSOLE_SET_STYLE:
392
 
393
			arg1 = IPC_GET_ARG1(call);
394
			arg2 = IPC_GET_ARG2(call);
1533 palkovsky 395
			screenbuffer_set_style(&(connections[consnum].screenbuffer),arg1, arg2);
1525 cejka 396
			if (consnum == active_console)
1552 palkovsky 397
				set_style_col(arg1, arg2);
1525 cejka 398
 
399
			break;
1453 palkovsky 400
		case CONSOLE_GETCHAR:
1481 cejka 401
			if (keybuffer_empty(&(connections[consnum].keybuffer))) {
402
				/* buffer is empty -> store request */
403
				if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {		
404
					fifo_push(connections[consnum].keyrequests, callid);
405
					connections[consnum].keyrequest_counter++;
406
				} else {
407
					/* no key available and too many requests => fail */
408
					ipc_answer_fast(callid, ELIMIT, 0, 0);
409
				}
410
				continue;
1453 palkovsky 411
			};
1476 cejka 412
			keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
1465 cejka 413
 
1453 palkovsky 414
			break;
415
		}
1521 cejka 416
		ipc_answer_fast(callid, 0, arg1, arg2);
1453 palkovsky 417
	}
418
}
419
 
420
int main(int argc, char *argv[])
421
{
422
	ipcarg_t phonehash;
1451 cejka 423
	int kbd_phone, fb_phone;
1445 cejka 424
	ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
1451 cejka 425
	int i;
1490 palkovsky 426
 
427
	async_set_client_connection(client_connection);
1445 cejka 428
 
429
	/* Connect to keyboard driver */
430
 
1451 cejka 431
	while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
1453 palkovsky 432
		usleep(10000);
1445 cejka 433
	};
434
 
1453 palkovsky 435
	if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 436
		return -1;
437
	};
438
 
439
	/* Connect to framebuffer driver */
440
 
1487 cejka 441
	while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
442
		usleep(10000);
443
	}
1552 palkovsky 444
 
445
	/* Save old kernel screen */
446
	kernel_pixmap = switch_screens(-1);
1522 palkovsky 447
 
448
	/* Initialize gcons */
449
	gcons_init(fb_info.phone);
450
	/* Synchronize, the gcons can have something in queue */
451
	sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
452
 
1487 cejka 453
 
454
	ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols)); 
1552 palkovsky 455
	set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
456
	clrscr();
1487 cejka 457
 
458
	/* Init virtual consoles */
1451 cejka 459
	for (i = 0; i < CONSOLE_COUNT; i++) {
460
		connections[i].used = 0;
461
		keybuffer_init(&(connections[i].keybuffer));
1481 cejka 462
 
463
		connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
464
		connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
465
		connections[i].keyrequest_counter = 0;
1487 cejka 466
 
467
		if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
468
			/*FIXME: handle error */
469
			return -1;
470
		}
1451 cejka 471
	}
1445 cejka 472
 
1512 cejka 473
	if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
1547 palkovsky 474
		if (ipc_call_sync_3(fb_info.phone, IPC_M_AS_AREA_SEND, (ipcarg_t)interbuffer, 0, AS_AREA_READ, NULL, NULL, NULL) != 0) {
1512 cejka 475
			munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
476
			interbuffer = NULL;
477
		}
478
	}
1520 cejka 479
 
1506 cejka 480
	async_new_connection(phonehash, 0, NULL, keyboard_events);
481
 
1522 palkovsky 482
	sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL); 
1520 cejka 483
	nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1); 
1497 cejka 484
 
1518 palkovsky 485
	/* Register at NS */
1453 palkovsky 486
	if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 487
		return -1;
488
	};
489
 
1453 palkovsky 490
	async_manager();
1451 cejka 491
 
1445 cejka 492
	return 0;	
493
}
1520 cejka 494