Skip to content

Commit

Permalink
select virtio pci bus per driver
Browse files Browse the repository at this point in the history
  • Loading branch information
arcnmx committed Aug 11, 2021
1 parent 39631da commit be32f41
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
37 changes: 26 additions & 11 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,46 @@ impl Default for ConfigQemu {
}

impl ConfigQemu {
pub fn keyboard_driver(&self) -> ConfigQemuDriver {
pub fn keyboard_driver(&self) -> &ConfigQemuDriver {
self.keyboard_driver
.or(self.driver)
.unwrap_or(ConfigQemuDriver::Ps2)
.as_ref()
.or(self.driver.as_ref())
.unwrap_or(&ConfigQemuDriver::Ps2)
}

pub fn relative_driver(&self) -> ConfigQemuDriver {
pub fn relative_driver(&self) -> &ConfigQemuDriver {
self.relative_driver
.or(self.driver)
.unwrap_or(ConfigQemuDriver::Usb)
.as_ref()
.or(self.driver.as_ref())
.unwrap_or(&ConfigQemuDriver::Usb)
}

pub fn absolute_driver(&self) -> ConfigQemuDriver {
pub fn absolute_driver(&self) -> &ConfigQemuDriver {
self.absolute_driver
.or(self.driver)
.unwrap_or(ConfigQemuDriver::Usb)
.as_ref()
.or(self.driver.as_ref())
.unwrap_or(&ConfigQemuDriver::Usb)
}
}

#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ConfigQemuDriver {
Ps2,
Usb,
Virtio,
Virtio {
#[serde(default, skip_serializing_if = "Option::is_none")]
bus: Option<String>,
},
}

impl ConfigQemuDriver {
pub fn bus(&self) -> Option<&String> {
match self {
ConfigQemuDriver::Virtio { bus } => bus.as_ref(),
_ => None,
}
}
}

#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async fn main_result(spawner: &Arc<Spawner>) -> Result<i32, Error> {
}).map_err(From::from);

let (keyboard_driver, relative_driver, absolute_driver) =
(config.qemu.keyboard_driver(), config.qemu.relative_driver(), config.qemu.absolute_driver());
(config.qemu.keyboard_driver().clone(), config.qemu.relative_driver().clone(), config.qemu.absolute_driver().clone());

let mut events = event::Events::new();
config.hotkeys.into_iter()
Expand All @@ -165,7 +165,7 @@ async fn main_result(spawner: &Arc<Spawner>) -> Result<i32, Error> {
let (error_sender, mut error_recv) = mpsc::channel(1);

let process = Process::new(
config.qemu.routing, keyboard_driver, relative_driver, absolute_driver, config.exit_events,
config.qemu.routing, keyboard_driver.clone(), relative_driver.clone(), absolute_driver.clone(), config.exit_events,
qemu.clone(), events.clone(), sources, xreq_sender.clone(), event_sender.clone(), error_sender.clone(),
spawner.clone(),
);
Expand All @@ -180,8 +180,7 @@ async fn main_result(spawner: &Arc<Spawner>) -> Result<i32, Error> {
};

let repeat = false;
let bus = None;
let mut route_keyboard = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-kbd".into(), bus.clone(), repeat);
let mut route_keyboard = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-kbd".into(), keyboard_driver.bus().cloned(), repeat);
if let Some(builder) = route_keyboard.builder() {
builder
.name("screenstub-kbd")
Expand All @@ -190,7 +189,7 @@ async fn main_result(spawner: &Arc<Spawner>) -> Result<i32, Error> {
}
let mut events_keyboard = route_keyboard.spawn(spawner, error_sender.clone());

let mut route_relative = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-mouse".into(), bus.clone(), repeat);
let mut route_relative = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-mouse".into(), relative_driver.bus().cloned(), repeat);
if let Some(builder) = route_relative.builder() {
builder
.name("screenstub-mouse")
Expand All @@ -199,7 +198,7 @@ async fn main_result(spawner: &Arc<Spawner>) -> Result<i32, Error> {
}
let mut events_relative = route_relative.spawn(spawner, error_sender.clone());

let mut route_absolute = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-tablet".into(), bus, repeat);
let mut route_absolute = Route::new(config.qemu.routing, qemu.clone(), "screenstub-route-tablet".into(), absolute_driver.bus().cloned(), repeat);
if let Some(builder) = route_absolute.builder() {
builder
.name("screenstub-tablet")
Expand Down
38 changes: 19 additions & 19 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl Drop for GrabHandle {

pub struct Process {
routing: ConfigQemuRouting,
driver_keyboard: ConfigQemuDriver,
driver_relative: ConfigQemuDriver,
driver_absolute: ConfigQemuDriver,
driver_keyboard: Arc<ConfigQemuDriver>,
driver_relative: Arc<ConfigQemuDriver>,
driver_absolute: Arc<ConfigQemuDriver>,
exit_events: Vec<config::ConfigEvent>,
qemu: Arc<Qemu>,
events: Arc<Events>,
Expand All @@ -64,9 +64,9 @@ impl Process {
pub fn new(routing: ConfigQemuRouting, driver_keyboard: ConfigQemuDriver, driver_relative: ConfigQemuDriver, driver_absolute: ConfigQemuDriver, exit_events: Vec<config::ConfigEvent>, qemu: Arc<Qemu>, events: Arc<Events>, sources: Sources, xreq_sender: un_mpsc::Sender<XRequest>, event_sender: un_mpsc::Sender<InputEvent>, error_sender: un_mpsc::Sender<Error>, spawner: Arc<Spawner>) -> Self {
Process {
routing,
driver_keyboard,
driver_relative,
driver_absolute,
driver_keyboard: Arc::new(driver_keyboard),
driver_relative: Arc::new(driver_relative),
driver_absolute: Arc::new(driver_absolute),
exit_events,
qemu,
events,
Expand Down Expand Up @@ -98,23 +98,23 @@ impl Process {
}
}

fn add_device_cmd(device: InputDevice, driver: ConfigQemuDriver) -> Option<qapi::qmp::device_add> {
fn add_device_cmd(device: InputDevice, driver: &ConfigQemuDriver) -> Option<qapi::qmp::device_add> {
let driver = match (device, driver) {
(InputDevice::Absolute, ConfigQemuDriver::Ps2) => panic!("PS/2 tablet not possible"),
(_, ConfigQemuDriver::Ps2) => return None,
(InputDevice::Keyboard, ConfigQemuDriver::Usb) => "usb-kbd",
(InputDevice::Relative, ConfigQemuDriver::Usb) => "usb-mouse",
(InputDevice::Absolute, ConfigQemuDriver::Usb) => "usb-tablet",
(InputDevice::Keyboard, ConfigQemuDriver::Virtio) => "virtio-keyboard-pci",
(InputDevice::Relative, ConfigQemuDriver::Virtio) => "virtio-mouse-pci",
(InputDevice::Absolute, ConfigQemuDriver::Virtio) => "virtio-tablet-pci",
(InputDevice::Keyboard, ConfigQemuDriver::Virtio { .. }) => "virtio-keyboard-pci",
(InputDevice::Relative, ConfigQemuDriver::Virtio { .. }) => "virtio-mouse-pci",
(InputDevice::Absolute, ConfigQemuDriver::Virtio { .. }) => "virtio-tablet-pci",
};

let id = Self::device_id(device);
Some(qapi::qmp::device_add::new(driver, Some(id.into()), None, Vec::new()))
}

async fn devices_init_cmd(qemu: Arc<Qemu>, routing: ConfigQemuRouting, device: InputDevice, driver: ConfigQemuDriver) -> Result<(), Error> {
async fn devices_init_cmd(qemu: Arc<Qemu>, routing: ConfigQemuRouting, device: InputDevice, driver: &ConfigQemuDriver) -> Result<(), Error> {
match routing {
ConfigQemuRouting::VirtioHost => return Ok(()),
_ => (),
Expand All @@ -128,24 +128,24 @@ impl Process {
}

pub async fn devices_init(&self) -> Result<(), Error> {
Self::devices_init_cmd(self.qemu.clone(), self.routing, InputDevice::Keyboard, self.driver_keyboard).await?;
Self::devices_init_cmd(self.qemu.clone(), self.routing, InputDevice::Keyboard, &self.driver_keyboard).await?;
self.set_is_mouse(false).await?; // TODO: config option to start up in relative mode instead

Ok(())
}

async fn set_is_mouse_cmd(qemu: Arc<Qemu>, routing: ConfigQemuRouting, driver_relative: ConfigQemuDriver, driver_absolute: ConfigQemuDriver, is_mouse: bool) -> Result<(), Error> {
async fn set_is_mouse_cmd(qemu: Arc<Qemu>, routing: ConfigQemuRouting, driver_relative: Arc<ConfigQemuDriver>, driver_absolute: Arc<ConfigQemuDriver>, is_mouse: bool) -> Result<(), Error> {
let (device, driver) = if is_mouse {
(InputDevice::Relative, driver_relative)
} else {
(InputDevice::Absolute, driver_absolute)
};

Self::devices_init_cmd(qemu, routing, device, driver).await
Self::devices_init_cmd(qemu, routing, device, &driver).await
}

pub fn set_is_mouse(&self, is_mouse: bool) -> impl Future<Output=Result<(), Error>> {
Self::set_is_mouse_cmd(self.qemu.clone(), self.routing, self.driver_relative, self.driver_absolute, is_mouse)
Self::set_is_mouse_cmd(self.qemu.clone(), self.routing, self.driver_relative.clone(), self.driver_absolute.clone(), is_mouse)
}

fn grab(&self, grab: &ConfigGrab) -> Pin<Box<dyn Future<Output=Result<(), Error>> + Send>> {
Expand All @@ -156,8 +156,8 @@ impl Process {
let qemu = self.qemu.clone();
let grabs = self.grabs.clone();
let routing = self.routing;
let driver_relative = self.driver_relative;
let driver_absolute = self.driver_absolute;
let driver_relative = self.driver_relative.clone();
let driver_absolute = self.driver_absolute.clone();
let prev_is_mouse = self.is_mouse();
let ignore = ignore.clone();
let x_filter = self.x_input_filter.clone();
Expand Down Expand Up @@ -196,8 +196,8 @@ impl Process {
let event_sender = if new_device_name.is_some() { None } else { Some(self.event_sender.clone()) };
let routing = self.routing;
let uinput_id = self.uinput_id.clone();
let driver_relative = self.driver_relative;
let driver_absolute = self.driver_absolute;
let driver_relative = self.driver_relative.clone();
let driver_absolute = self.driver_absolute.clone();
let prev_is_mouse = self.is_mouse();
let spawner = self.spawner.clone();
let grab = GrabEvdev::new(devices, evdev_ignore.iter().cloned());
Expand Down
6 changes: 1 addition & 5 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ pub struct RouteUInputVirtio {

impl UInputCommands for RouteUInputVirtio {
fn command_create(&self, qemu: &Arc<Qemu>, path: &Path) -> Pin<Box<dyn Future<Output=Result<(), Error>> + Send>> {
let name = match self.bus.is_some() {
true => "virtio-input-host-device", // TODO: double-check this, what is the virtio bus for?
false => "virtio-input-host-pci",
};
let command = qmp::device_add::new(name, Some(self.id.clone()), self.bus.clone(), vec![
let command = qmp::device_add::new("virtio-input-host-pci", Some(self.id.clone()), self.bus.clone(), vec![
("evdev".into(), Any::String(path.display().to_string())),
("multifunction".into(), Any::Bool(true)),
]);
Expand Down

0 comments on commit be32f41

Please sign in to comment.