#endif
diff --git a/dom0_driver.c b/dom0_driver.c
new file mode 100644
-index 0000000..fdd24ab
+index 0000000..05f7cc4
--- /dev/null
+++ b/dom0_driver.c
-@@ -0,0 +1,697 @@
+@@ -0,0 +1,705 @@
+/*
+ * QEMU dom0_driver
+ *
+ natif_domid = dom0_driver_xs_read_dom0("natif");
+
+ dom0_driver_read_xs_info(&mouse, DOM0_MOUSE);
-+ if (mouse.domid != natif_domid)
-+ {
-+ DEBUG("Natif should have the focus for that.\n");
-+ return;
-+ }
-+
+ for (i = 0; i < NB_SLOTS; ++i)
+ {
++ if (!strcmp("switch", str) && positions[i] == domid)
++ break;
+ if (!strcmp("take", str) && positions[i] == domid)
+ break;
+ if (!strcmp("release", str) && positions[i] == natif_domid)
+ return;
+ }
+
-+ sprintf(buff, "keyboard %d", i);
++ if (!strcmp("switch", str))
++ sprintf(buff, "switch %d", i);
++ else
++ {
++ if (mouse.domid != natif_domid)
++ {
++ DEBUG("Natif should have the focus for that.\n");
++ return;
++ }
++ sprintf(buff, "keyboard %d", i);
++ }
++
+ DEBUG("Write \"%s\" into xenstore\n", buff);
+ xenstore_write_dom0_driver("command", buff);
+ free(str);
+ xenstore_write_dom0_driver("natif", str);
+ xenstore_dom_chmod(0, "dom0_driver/natif", "r0");
+ xenstore_watch_dom0_driver("command", dom0_driver_command, NULL);
-+ }
++ xenstore_dom_chmod(0, "dom0_driver/command", "w0");
++ }
+ else
+ {
-+ xenstore_dom_write(domid, "dom0_input/command", "");
-+ sprintf(str, "w%d", domid);
-+ xenstore_dom_chmod(domid, "dom0_input/command", str);
-+ xenstore_dom_watch(domid, "dom0_input/command", dom0_driver_dom_command, NULL);
++ xenstore_dom_write(domid, "dom0_input/command", "");
++ sprintf(str, "w%d", domid);
++ xenstore_dom_chmod(domid, "dom0_input/command", str);
++ xenstore_dom_watch(domid, "dom0_input/command", dom0_driver_dom_command, NULL);
+ }
+
+ xenstore_watch_dom0_driver("mouse/state", dom0_driver_state_change, NULL);
+ }
+}
diff --git a/qemu-xen.h b/qemu-xen.h
-index 0cc5dd8..2646ec7 100644
+index 7883718..afe8f22 100644
--- a/qemu-xen.h
+++ b/qemu-xen.h
-@@ -107,7 +107,13 @@ int xenstore_write(const char *path, const char *val);
+@@ -109,7 +109,13 @@ int xenstore_write(const char *path, const char *val);
void xenstore_dm_finished_startup(void);
* not be trusted by qemu code. For variables containing xenstore
* paths, `danger' can mean that both the path refers to a
diff --git a/vl.c b/vl.c
-index e519705..0ffe1ec 100644
+index 6d79072..adfa4f6 100644
--- a/vl.c
+++ b/vl.c
@@ -234,6 +234,7 @@ CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
/***********************************************************/
/* x86 ISA bus support */
-@@ -4277,6 +4292,7 @@ enum {
+@@ -4289,6 +4304,7 @@ enum {
QEMU_OPTION_acpi,
QEMU_OPTION_vcpus,
QEMU_OPTION_vga_passthrough,
/* Debug/Expert options: */
QEMU_OPTION_serial,
-@@ -4451,6 +4467,7 @@ static const QEMUOption qemu_options[] = {
+@@ -4463,6 +4479,7 @@ static const QEMUOption qemu_options[] = {
{ "vncunused", 0, QEMU_OPTION_vncunused },
{ "vcpus", HAS_ARG, QEMU_OPTION_vcpus },
{ "vga-passthrough", 0, QEMU_OPTION_vga_passthrough },
#if defined(CONFIG_XEN) && !defined(CONFIG_DM)
{ "xen-domid", HAS_ARG, QEMU_OPTION_xen_domid },
{ "xen-create", 0, QEMU_OPTION_xen_create },
-@@ -5287,6 +5304,9 @@ int main(int argc, char **argv, char **envp)
+@@ -5299,6 +5316,9 @@ int main(int argc, char **argv, char **envp)
case QEMU_OPTION_vga_passthrough:
vga_passthrough = 1;
break;
case QEMU_OPTION_direct_pci:
direct_pci = optarg;
break;
-@@ -6053,6 +6073,9 @@ int main(int argc, char **argv, char **envp)
+@@ -6067,6 +6087,9 @@ int main(int argc, char **argv, char **envp)
close(fd);
}
+
xenstore_dm_finished_startup();
- main_loop();
+ #ifndef CONFIG_STUBDOM
diff --git a/xen-hooks.mak b/xen-hooks.mak
-index d171928..15ba4d1 100644
+index 799f80d..c0212a2 100644
--- a/xen-hooks.mak
+++ b/xen-hooks.mak
@@ -37,6 +37,8 @@ OBJS += helper2.o
CONFIG_AUDIO=1
diff --git a/xenstore.c b/xenstore.c
-index 01afcf0..20ca8cf 100644
+index 3cd2ba6..17136a3 100644
--- a/xenstore.c
+++ b/xenstore.c
-@@ -1727,3 +1727,63 @@ void xenstore_dm_finished_startup(void)
+@@ -1741,3 +1741,63 @@ void xenstore_dm_finished_startup(void)
free(buf);
free(path);
}