Subversion Repositories HelenOS

Rev

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