Subversion Repositories HelenOS

Rev

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