Index: uspace/srv/hid/input/input.c
===================================================================
--- uspace/srv/hid/input/input.c	(revision ce3efa072661e7cca5ddc305768f65fce320e41c)
+++ uspace/srv/hid/input/input.c	(revision 593e0237afd3144ed94431340aab1dd29fc7f612)
@@ -54,4 +54,6 @@
 #include <io/keycode.h>
 #include <loc.h>
+#include <event.h>
+#include <str_error.h>
 #include "layout.h"
 #include "kbd.h"
@@ -62,4 +64,7 @@
 #include "input.h"
 
+bool irc_service = false;
+async_sess_t *irc_sess = NULL;
+
 #define NUM_LAYOUTS  4
 
@@ -71,8 +76,18 @@
 };
 
-static void kbd_devs_yield(void);
-static void kbd_devs_reclaim(void);
-
-async_sess_t *client_sess = NULL;
+typedef struct {
+	/** Link into the list of clients */
+	link_t link;
+	
+	/** Indicate whether the client is active */
+	bool active;
+	
+	/** Client callback session */
+	async_sess_t *sess;
+} client_t;
+
+/** List of clients */
+static list_t clients;
+static client_t *active_client = NULL;
 
 /** List of keyboard devices */
@@ -82,8 +97,28 @@
 static list_t mouse_devs;
 
-bool irc_service = false;
-async_sess_t *irc_sess = NULL;
-
 static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
+
+static void *client_data_create(void)
+{
+	client_t *client = (client_t *) calloc(1, sizeof(client_t));
+	if (client == NULL)
+		return NULL;
+	
+	link_initialize(&client->link);
+	client->active = false;
+	client->sess = NULL;
+	
+	list_append(&client->link, &clients);
+	
+	return client;
+}
+
+static void client_data_destroy(void *data)
+{
+	client_t *client = (client_t *) data;
+	
+	list_remove(&client->link);
+	free(client);
+}
 
 void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
@@ -199,34 +234,50 @@
 	ev.c = layout_parse_ev(kdev->active_layout, &ev);
 	
-	async_exch_t *exch = async_exchange_begin(client_sess);
-	async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
-	async_exchange_end(exch);
-}
-
-/** Mouse pointer has moved. */
+	list_foreach(clients, link, client_t, client) {
+		if (client->active) {
+			async_exch_t *exch = async_exchange_begin(client->sess);
+			async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
+			async_exchange_end(exch);
+		}
+	}
+}
+
+/** Mouse pointer has moved (relative mode). */
 void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
 {
-	async_exch_t *exch = async_exchange_begin(client_sess);
-	if (dx || dy)
-		async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
-	if (dz) {
-		// TODO: Implement proper wheel support
-		keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
-		for (int i = 0; i < 3; ++i) {
-			async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
-		}
-		async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
-	}
-	async_exchange_end(exch);
-}
-
-/** Mouse pointer has moved in absolute mode. */
+	list_foreach(clients, link, client_t, client) {
+		if (client->active) {
+			async_exch_t *exch = async_exchange_begin(client->sess);
+			
+			if ((dx) || (dy))
+				async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
+			
+			if (dz) {
+				// TODO: Implement proper wheel support
+				keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
+				
+				for (unsigned int i = 0; i < 3; i++)
+					async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
+				
+				async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
+			}
+			
+			async_exchange_end(exch);
+		}
+	}
+}
+
+/** Mouse pointer has moved (absolute mode). */
 void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
     unsigned int max_x, unsigned int max_y)
 {
-	if (max_x && max_y) {
-		async_exch_t *exch = async_exchange_begin(client_sess);
-		async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
-		async_exchange_end(exch);
+	list_foreach(clients, link, client_t, client) {
+		if (client->active) {
+			if ((max_x) && (max_y)) {
+				async_exch_t *exch = async_exchange_begin(client->sess);
+				async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
+				async_exchange_end(exch);
+			}
+		}
 	}
 }
@@ -235,11 +286,38 @@
 void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
 {
-	async_exch_t *exch = async_exchange_begin(client_sess);
-	async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
-	async_exchange_end(exch);
-}
-
+	list_foreach(clients, link, client_t, client) {
+		if (client->active) {
+			async_exch_t *exch = async_exchange_begin(client->sess);
+			async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
+			async_exchange_end(exch);
+		}
+	}
+}
+
+/** Arbitrate client actiovation */
+static void client_arbitration(client_t *req)
+{
+	/* Mutual exclusion of active clients */
+	list_foreach(clients, link, client_t, client)
+		client->active = (client == req);
+	
+	/* Notify clients about the arbitration */
+	list_foreach(clients, link, client_t, client) {
+		async_exch_t *exch = async_exchange_begin(client->sess);
+		async_msg_0(exch, client->active ?
+		    INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
+		async_exchange_end(exch);
+	}
+}
+
+/** New client connection */
 static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
 {
+	client_t *client = (client_t *) async_get_client_data();
+	if (client == NULL) {
+		async_answer_0(iid, ENOMEM);
+		return;
+	}
+	
 	async_answer_0(iid, EOK);
 	
@@ -249,7 +327,7 @@
 		
 		if (!IPC_GET_IMETHOD(call)) {
-			if (client_sess != NULL) {
-				async_hangup(client_sess);
-				client_sess = NULL;
+			if (client->sess != NULL) {
+				async_hangup(client->sess);
+				client->sess = NULL;
 			}
 			
@@ -261,6 +339,6 @@
 		    async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
 		if (sess != NULL) {
-			if (client_sess == NULL) {
-				client_sess = sess;
+			if (client->sess == NULL) {
+				client->sess = sess;
 				async_answer_0(callid, EOK);
 			} else
@@ -268,10 +346,7 @@
 		} else {
 			switch (IPC_GET_IMETHOD(call)) {
-			case INPUT_YIELD:
-				kbd_devs_yield();
-				async_answer_0(callid, EOK);
-				break;
-			case INPUT_RECLAIM:
-				kbd_devs_reclaim();
+			case INPUT_ACTIVATE:
+				active_client = client;
+				client_arbitration(client);
 				async_answer_0(callid, EOK);
 				break;
@@ -283,4 +358,15 @@
 }
 
+static void kconsole_event_received(ipc_callid_t callid, ipc_call_t *call)
+{
+	if (IPC_GET_ARG1(*call)) {
+		/* Kernel console activated */
+		client_arbitration(NULL);
+	} else {
+		/* Kernel console deactivated */
+		client_arbitration(active_client);
+	}
+}
+
 static kbd_dev_t *kbd_dev_new(void)
 {
@@ -292,5 +378,5 @@
 	}
 	
-	link_initialize(&kdev->kbd_devs);
+	link_initialize(&kdev->link);
 	
 	kdev->mods = KM_NUM_LOCK;
@@ -310,5 +396,5 @@
 	}
 	
-	link_initialize(&mdev->mouse_devs);
+	link_initialize(&mdev->link);
 	
 	return mdev;
@@ -336,5 +422,5 @@
 	}
 	
-	list_append(&kdev->kbd_devs, &kbd_devs);
+	list_append(&kdev->link, &kbd_devs);
 	return;
 	
@@ -364,5 +450,5 @@
 	}
 	
-	list_append(&mdev->mouse_devs, &mouse_devs);
+	list_append(&mdev->link, &mouse_devs);
 	return;
 	
@@ -373,5 +459,5 @@
 /** Add new kbdev device.
  *
- * @param service_id	Service ID of the keyboard device
+ * @param service_id Service ID of the keyboard device
  *
  */
@@ -397,5 +483,5 @@
 	}
 	
-	list_append(&kdev->kbd_devs, &kbd_devs);
+	list_append(&kdev->link, &kbd_devs);
 	*kdevp = kdev;
 	return EOK;
@@ -410,5 +496,5 @@
 /** Add new mousedev device.
  *
- * @param service_id	Service ID of the mouse device
+ * @param service_id Service ID of the mouse device
  *
  */
@@ -434,5 +520,5 @@
 	}
 	
-	list_append(&mdev->mouse_devs, &mouse_devs);
+	list_append(&mdev->link, &mouse_devs);
 	*mdevp = mdev;
 	return EOK;
@@ -489,24 +575,4 @@
 }
 
-static void kbd_devs_yield(void)
-{
-	/* For each keyboard device */
-	list_foreach(kbd_devs, kbd_devs, kbd_dev_t, kdev) {
-		/* Yield port */
-		if (kdev->port_ops != NULL)
-			(*kdev->port_ops->yield)();
-	}
-}
-
-static void kbd_devs_reclaim(void)
-{
-	/* For each keyboard device */
-	list_foreach(kbd_devs, kbd_devs, kbd_dev_t, kdev) {
-		/* Reclaim port */
-		if (kdev->port_ops != NULL)
-			(*kdev->port_ops->reclaim)();
-	}
-}
-
 static int dev_check_new_kbdevs(void)
 {
@@ -537,5 +603,5 @@
 		
 		/* Determine whether we already know this device. */
-		list_foreach(kbd_devs, kbd_devs, kbd_dev_t, kdev) {
+		list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
 			if (kdev->svc_id == svcs[i]) {
 				already_known = true;
@@ -588,5 +654,5 @@
 		
 		/* Determine whether we already know this device. */
-		list_foreach(mouse_devs, mouse_devs, mouse_dev_t, mdev) {
+		list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
 			if (mdev->svc_id == svcs[i]) {
 				already_known = true;
@@ -668,4 +734,5 @@
 	sysarg_t obio;
 	
+	list_initialize(&clients);
 	list_initialize(&kbd_devs);
 	list_initialize(&mouse_devs);
@@ -687,4 +754,6 @@
 	
 	/* Register driver */
+	async_set_client_data_constructor(client_data_create);
+	async_set_client_data_destructor(client_data_destroy);
 	async_set_client_connection(client_connection);
 	
@@ -701,4 +770,11 @@
 		return rc;
 	}
+	
+	/* Receive kernel notifications */
+	async_set_interrupt_received(kconsole_event_received);
+	rc = event_subscribe(EVENT_KCONSOLE, 0);
+	if (rc != EOK)
+		printf("%s: Failed to register kconsole notifications (%s)\n",
+		    NAME, str_error(rc));
 	
 	/* Start looking for new input devices */
