Subversion Repositories HelenOS

Rev

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