Index: console/console.c
===================================================================
--- console/console.c	(revision 854387b48c1405befa1109b49cef5c35e39767b3)
+++ console/console.c	(revision 44c6d88d97fb845af719b8a78de9f944baccc78e)
@@ -29,10 +29,51 @@
 
 #include <kbd.h>
+#include <fb.h>
 #include <ipc/ipc.h>
+#include <ipc/fb.h>
 #include <ipc/services.h>
-#include <stdio.h>
 #include <errno.h>
+#include <key_buffer.h>
+#include <console.h>
+
+//#define CONSOLE_COUNT VFB_CONNECTIONS
+#define CONSOLE_COUNT 6
 
 #define NAME "CONSOLE"
+
+typedef struct {
+	keybuffer_t keybuffer;
+	int client_phone;
+	int vfb_number;	/* Not used */
+	int vfb_phone;
+	int used;
+} connection_t;
+
+connection_t connections[CONSOLE_COUNT];
+
+static int find_free_connection() 
+{
+	int i = 0;
+	
+	while (i < CONSOLE_COUNT) {
+		if (connections[i].used == 0)
+			return i;
+		++i;
+	}
+	return CONSOLE_COUNT;
+}
+
+
+static int find_connection(int client_phone) 
+{
+	int i = 0;
+	
+	while (i < CONSOLE_COUNT) {
+		if (connections[i].client_phone == client_phone)
+			return i;
+		++i;
+	}
+	return  CONSOLE_COUNT;
+}
 
 int main(int argc, char *argv[])
@@ -41,17 +82,15 @@
 	ipc_call_t call;
 	ipc_callid_t callid;
-	int phone_kbd, phone_fb;
+	int kbd_phone, fb_phone;
 	ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
-	
-	printf("Uspace console service started.\n");
-	
+	int i;
+	int active_client = 0;
 	
 	/* Connect to keyboard driver */
 
-	while ((phone_kbd = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
+	while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
 	};
 	
-	if (ipc_connect_to_me(phone_kbd, SERVICE_CONSOLE, 0, &phonead) != 0) {
-		printf("%s: Error: Registering at naming service failed.\n", NAME);
+	if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonead) != 0) {
 		return -1;
 	};
@@ -59,13 +98,17 @@
 	/* Connect to framebuffer driver */
 	
-	while ((phone_fb = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
-	};
+	for (i = 0; i < CONSOLE_COUNT; i++) {
+		connections[i].used = 0;
+		keybuffer_init(&(connections[i].keybuffer));
+		/* TODO: init key_buffer */
+		while ((connections[i].vfb_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
+				
+ipc_call_async_2(connections[i].vfb_phone, FB_PUTCHAR, 'a', 'b', NULL, (void *)NULL); 
+		}
+	}
 	
+
 	
-	/* Register service at nameserver */
-	printf("%s: Registering at naming service.\n", NAME);
-
 	if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonead) != 0) {
-		printf("%s: Error: Registering at naming service failed.\n", NAME);
 		return -1;
 	};
@@ -75,18 +118,69 @@
 		switch (IPC_GET_METHOD(call)) {
 			case IPC_M_PHONE_HUNGUP:
-				printf("%s: Phone hung up.\n", NAME);
-				retval = 0;
+				/*FIXME: if its fb or kbd then panic! */
+				/* free connection */
+				if (i = find_connection(IPC_GET_ARG3(call)) < CONSOLE_COUNT) {
+					connections[i].used = 0;
+					 /*TODO: free connection[i].key_buffer; */
+					/* FIXME: active_connection hungup */
+					retval = 0;
+				} else {
+					/*FIXME: No such connection */
+				}
 				break;
 			case IPC_M_CONNECT_ME_TO:
-				printf("%s: Connect me (%P) to: %zd\n",NAME, IPC_GET_ARG3(call), IPC_GET_ARG1(call));
+				
+				/* find first free connection */
+				
+				if ((i = find_free_connection()) == CONSOLE_COUNT) {
+					retval = ELIMIT;
+					break;
+				}
+				
+				connections[i].used = 1;
+				connections[i].client_phone = IPC_GET_ARG3(call);
+				
 				retval = 0;
 				break;
 			case KBD_PUSHCHAR:
-				printf("%s: Push char '%c'.\n", NAME, IPC_GET_ARG1(call));
+				/* got key from keyboard driver */
+				/* find active console */
+				/* if client is awaiting key, send it */
+				/*FIXME: else store key to its buffer */
 				retval = 0;
+				i = IPC_GET_ARG1(call) & 0xff;
+				/* switch to another virtual console */
+				if ((i >= KBD_KEY_F1) && (i < KBD_KEY_F1 + CONSOLE_COUNT)) {
+					active_client = i - KBD_KEY_F1;
+					break;
+				}
+				
+				keybuffer_push(&(connections[active_client].keybuffer), i);
 				
 				break;
+			case CONSOLE_PUTCHAR:
+				/* find sender client */
+				/* ???
+				 * if its active client, send it to vfb
+				 **/
+				/*FIXME: check, if its from active client, .... */
+
+				if ((i = find_connection(IPC_GET_ARG3(call))) == CONSOLE_COUNT) {
+					break;
+				};
+				
+				/* TODO: send message to fb */
+				ipc_call_async_2(connections[i].vfb_phone, FB_PUTCHAR, IPC_GET_ARG1(call), IPC_GET_ARG2(call), NULL, NULL); 
+				
+				break;
+			case CONSOLE_GETCHAR:
+				/* FIXME: */
+				if (!keybuffer_pop(&(connections[active_client].keybuffer), (char *)&arg1)) {
+					/* FIXME: buffer empty -> store request */
+					arg1 = 'X'; /* Only temporary */
+				};
+//ipc_call_async_2(connections[active_client].vfb_phone, FB_PUTCHAR, ' ', arg1, NULL, (void *)NULL); 
+				break;
 			default:
-				printf("%s: Unknown method: %zd\n", NAME, IPC_GET_METHOD(call));
 				retval = ENOENT;
 				break;
