From 5210b39c355789329f82a981424e36fdaaa1f083 Mon Sep 17 00:00:00 2001 From: i509VCB Date: Thu, 28 Oct 2021 16:01:40 -0500 Subject: [PATCH] anvil: deduplicate some input handler logic --- anvil/src/input_handler.rs | 208 +++++++++++++------------------------ anvil/src/winit.rs | 6 +- anvil/src/x11.rs | 2 +- 3 files changed, 76 insertions(+), 140 deletions(-) diff --git a/anvil/src/input_handler.rs b/anvil/src/input_handler.rs index 2f8d290..79e54eb 100644 --- a/anvil/src/input_handler.rs +++ b/anvil/src/input_handler.rs @@ -4,10 +4,6 @@ use crate::AnvilState; #[cfg(feature = "udev")] use crate::udev::UdevData; -#[cfg(feature = "winit")] -use crate::winit::WinitData; -#[cfg(feature = "x11")] -use crate::x11::X11Data; use smithay::{ backend::input::{ @@ -38,6 +34,34 @@ use smithay::{ }; impl AnvilState { + fn process_common_key_action(&mut self, action: KeyAction) { + match action { + KeyAction::None => (), + + KeyAction::Quit => { + info!(self.log, "Quitting."); + self.running.store(false, Ordering::SeqCst); + } + + KeyAction::Run(cmd) => { + info!(self.log, "Starting program"; "cmd" => cmd.clone()); + + if let Err(e) = Command::new(&cmd).spawn() { + error!(self.log, + "Failed to start program"; + "cmd" => cmd, + "err" => format!("{:?}", e) + ); + } + } + + _ => unreachable!( + "Common key action handler encountered backend specific action {:?}", + action + ), + } + } + fn keyboard_key_to_action(&mut self, evt: B::KeyboardKeyEvent) -> KeyAction { let keycode = evt.key_code(); let state = evt.state(); @@ -143,72 +167,72 @@ impl AnvilState { } } -#[cfg(feature = "winit")] -impl AnvilState { - pub fn process_input_event(&mut self, event: InputEvent) { +#[cfg(any(feature = "winit", feature = "x11"))] +impl AnvilState { + pub fn process_input_event_windowed(&mut self, event: InputEvent, output_name: &str) { match event { - InputEvent::Keyboard { event, .. } => match self.keyboard_key_to_action::(event) { - KeyAction::None => {} - KeyAction::Quit => { - info!(self.log, "Quitting."); - self.running.store(false, Ordering::SeqCst); - } - KeyAction::Run(cmd) => { - info!(self.log, "Starting program"; "cmd" => cmd.clone()); - if let Err(e) = Command::new(&cmd).spawn() { - error!(self.log, - "Failed to start program"; - "cmd" => cmd, - "err" => format!("{:?}", e) - ); - } - } + InputEvent::Keyboard { event } => match self.keyboard_key_to_action::(event) { KeyAction::ScaleUp => { let current_scale = { self.output_map .borrow() - .find_by_name(crate::winit::OUTPUT_NAME) + .find_by_name(output_name) .map(|o| o.scale()) .unwrap_or(1.0) }; + self.output_map .borrow_mut() - .update_scale_by_name(current_scale + 0.25f32, crate::winit::OUTPUT_NAME); + .update_scale_by_name(current_scale + 0.25f32, output_name); } + KeyAction::ScaleDown => { let current_scale = { self.output_map .borrow() - .find_by_name(crate::winit::OUTPUT_NAME) + .find_by_name(output_name) .map(|o| o.scale()) .unwrap_or(1.0) }; - self.output_map.borrow_mut().update_scale_by_name( - f32::max(1.0f32, current_scale - 0.25f32), - crate::winit::OUTPUT_NAME, - ); - } - action => { - warn!(self.log, "Key action {:?} unsupported on winit backend.", action); + self.output_map + .borrow_mut() + .update_scale_by_name(f32::max(1.0f32, current_scale + 0.25f32), output_name); } + + action => match action { + KeyAction::None | KeyAction::Quit | KeyAction::Run(_) => { + self.process_common_key_action(action) + } + + _ => warn!( + self.log, + "Key action {:?} unsupported on on output {} backend.", action, output_name + ), + }, }, - InputEvent::PointerMotionAbsolute { event, .. } => self.on_pointer_move_absolute::(event), - InputEvent::PointerButton { event, .. } => self.on_pointer_button::(event), - InputEvent::PointerAxis { event, .. } => self.on_pointer_axis::(event), - _ => { - // other events are not handled in anvil (yet) + + InputEvent::PointerMotionAbsolute { event } => { + self.on_pointer_move_absolute_windowed::(event, output_name) } + InputEvent::PointerButton { event } => self.on_pointer_button::(event), + InputEvent::PointerAxis { event } => self.on_pointer_axis::(event), + _ => (), // other events are not handled in anvil (yet) } } - fn on_pointer_move_absolute(&mut self, evt: B::PointerMotionAbsoluteEvent) { + fn on_pointer_move_absolute_windowed( + &mut self, + evt: B::PointerMotionAbsoluteEvent, + output_name: &str, + ) { let output_size = self .output_map .borrow() - .find_by_name(crate::winit::OUTPUT_NAME) + .find_by_name(output_name) .map(|o| o.size()) .unwrap(); + let pos = evt.position_transformed(output_size); self.pointer_location = pos; let serial = SCOUNTER.next_serial(); @@ -222,11 +246,6 @@ impl AnvilState { pub fn process_input_event(&mut self, event: InputEvent) { match event { InputEvent::Keyboard { event, .. } => match self.keyboard_key_to_action::(event) { - KeyAction::None => {} - KeyAction::Quit => { - info!(self.log, "Quitting."); - self.running.store(false, Ordering::SeqCst); - } #[cfg(feature = "udev")] KeyAction::VtSwitch(vt) => { info!(self.log, "Trying to switch to vt {}", vt); @@ -234,16 +253,6 @@ impl AnvilState { error!(self.log, "Error switching to vt {}: {}", vt, err); } } - KeyAction::Run(cmd) => { - info!(self.log, "Starting program"; "cmd" => cmd.clone()); - if let Err(e) = Command::new(&cmd).spawn() { - error!(self.log, - "Failed to start program"; - "cmd" => cmd, - "err" => format!("{:?}", e) - ); - } - } KeyAction::Screen(num) => { let geometry = self.output_map.borrow().find_by_index(num).map(|o| o.geometry()); @@ -300,6 +309,14 @@ impl AnvilState { .motion(self.pointer_location, under, SCOUNTER.next_serial(), 0); } } + + action => match action { + KeyAction::None | KeyAction::Quit | KeyAction::Run(_) => { + self.process_common_key_action(action) + } + + _ => unreachable!(), + }, }, InputEvent::PointerMotion { event, .. } => self.on_pointer_move::(event), InputEvent::PointerButton { event, .. } => self.on_pointer_button::(event), @@ -486,87 +503,6 @@ impl AnvilState { } } -#[cfg(feature = "x11")] -impl AnvilState { - pub fn process_input_event(&mut self, event: InputEvent) { - match event { - InputEvent::Keyboard { event } => match self.keyboard_key_to_action::(event) { - KeyAction::None => (), - - KeyAction::Quit => { - info!(self.log, "Quitting."); - self.running.store(false, Ordering::SeqCst); - } - - KeyAction::Run(cmd) => { - info!(self.log, "Starting program"; "cmd" => cmd.clone()); - - if let Err(e) = Command::new(&cmd).spawn() { - error!(self.log, - "Failed to start program"; - "cmd" => cmd, - "err" => format!("{:?}", e) - ); - } - } - - KeyAction::ScaleUp => { - let current_scale = { - self.output_map - .borrow() - .find_by_name(crate::x11::OUTPUT_NAME) - .map(|o| o.scale()) - .unwrap_or(1.0) - }; - - self.output_map - .borrow_mut() - .update_scale_by_name(current_scale + 0.25f32, crate::x11::OUTPUT_NAME); - } - - KeyAction::ScaleDown => { - let current_scale = { - self.output_map - .borrow() - .find_by_name(crate::x11::OUTPUT_NAME) - .map(|o| o.scale()) - .unwrap_or(1.0) - }; - - self.output_map.borrow_mut().update_scale_by_name( - f32::max(1.0f32, current_scale + 0.25f32), - crate::x11::OUTPUT_NAME, - ); - } - - action => { - warn!(self.log, "Key action {:?} unsupported on x11 backend.", action); - } - }, - - InputEvent::PointerMotionAbsolute { event } => self.on_pointer_move_absolute::(event), - InputEvent::PointerButton { event } => self.on_pointer_button::(event), - InputEvent::PointerAxis { event } => self.on_pointer_axis::(event), - _ => (), // other events are not handled in anvil (yet) - } - } - - fn on_pointer_move_absolute(&mut self, evt: B::PointerMotionAbsoluteEvent) { - let output_size = self - .output_map - .borrow() - .find_by_name(crate::x11::OUTPUT_NAME) - .map(|o| o.size()) - .unwrap(); - - let pos = evt.position_transformed(output_size); - self.pointer_location = pos; - let serial = SCOUNTER.next_serial(); - let under = self.window_map.borrow().get_surface_under(pos); - self.pointer.motion(pos, under, serial, evt.time()); - } -} - /// Possible results of a keyboard action #[derive(Debug)] enum KeyAction { diff --git a/anvil/src/winit.rs b/anvil/src/winit.rs index ddcf30e..a137cf4 100644 --- a/anvil/src/winit.rs +++ b/anvil/src/winit.rs @@ -132,16 +132,16 @@ pub fn run_winit(log: Logger) { size, refresh: 60_000, }, - crate::winit::OUTPUT_NAME, + OUTPUT_NAME, ); let output_mut = state.output_map.borrow(); - let output = output_mut.find_by_name(crate::winit::OUTPUT_NAME).unwrap(); + let output = output_mut.find_by_name(OUTPUT_NAME).unwrap(); state.window_map.borrow_mut().layers.arange_layers(output); } - WinitEvent::Input(event) => state.process_input_event(event), + WinitEvent::Input(event) => state.process_input_event_windowed(event, OUTPUT_NAME), _ => (), }) diff --git a/anvil/src/x11.rs b/anvil/src/x11.rs index f12cf3e..d06ae83 100644 --- a/anvil/src/x11.rs +++ b/anvil/src/x11.rs @@ -164,7 +164,7 @@ pub fn run_x11(log: Logger) { state.backend_data.render = true; } - X11Event::Input(event) => state.process_input_event(event), + X11Event::Input(event) => state.process_input_event_windowed(event, OUTPUT_NAME), }) .expect("Failed to insert X11 Backend into event loop");