2020-05-04 13:04:30 +00:00
|
|
|
use std::{process::Command, sync::atomic::Ordering};
|
2018-05-08 10:47:09 +00:00
|
|
|
|
2021-06-05 17:58:51 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
use crate::udev::UdevData;
|
|
|
|
#[cfg(feature = "winit")]
|
|
|
|
use crate::winit::WinitData;
|
2021-06-09 20:12:35 +00:00
|
|
|
use crate::AnvilState;
|
2018-05-08 10:47:09 +00:00
|
|
|
|
2018-10-02 21:37:24 +00:00
|
|
|
use smithay::{
|
|
|
|
backend::input::{
|
2020-05-04 11:03:36 +00:00
|
|
|
self, Event, InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent,
|
2018-10-02 21:37:24 +00:00
|
|
|
PointerButtonEvent, PointerMotionAbsoluteEvent, PointerMotionEvent,
|
|
|
|
},
|
2018-12-15 21:01:24 +00:00
|
|
|
reexports::wayland_server::protocol::wl_pointer,
|
2018-10-04 21:37:45 +00:00
|
|
|
wayland::{
|
2020-05-04 13:04:30 +00:00
|
|
|
seat::{keysyms as xkb, AxisFrame, Keysym, ModifiersState},
|
2018-10-04 21:37:45 +00:00
|
|
|
SERIAL_COUNTER as SCOUNTER,
|
|
|
|
},
|
2018-10-02 21:37:24 +00:00
|
|
|
};
|
2018-05-08 10:47:09 +00:00
|
|
|
|
2021-05-31 19:06:58 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
use smithay::backend::session::Session;
|
|
|
|
|
2021-05-30 20:01:36 +00:00
|
|
|
impl<Backend> AnvilState<Backend> {
|
2021-05-30 22:56:37 +00:00
|
|
|
fn keyboard_key_to_action<B: InputBackend>(&mut self, evt: B::KeyboardKeyEvent) -> KeyAction {
|
2018-05-08 10:47:09 +00:00
|
|
|
let keycode = evt.key_code();
|
|
|
|
let state = evt.state();
|
|
|
|
debug!(self.log, "key"; "keycode" => keycode, "state" => format!("{:?}", state));
|
2018-10-04 21:37:45 +00:00
|
|
|
let serial = SCOUNTER.next_serial();
|
2018-05-08 10:47:09 +00:00
|
|
|
let log = &self.log;
|
|
|
|
let time = Event::time(&evt);
|
|
|
|
let mut action = KeyAction::None;
|
|
|
|
self.keyboard
|
|
|
|
.input(keycode, state, serial, time, |modifiers, keysym| {
|
|
|
|
debug!(log, "keysym";
|
|
|
|
"state" => format!("{:?}", state),
|
|
|
|
"mods" => format!("{:?}", modifiers),
|
|
|
|
"keysym" => ::xkbcommon::xkb::keysym_get_name(keysym)
|
|
|
|
);
|
2020-01-01 10:43:16 +00:00
|
|
|
action = process_keyboard_shortcut(*modifiers, keysym);
|
2018-05-08 10:47:09 +00:00
|
|
|
// forward to client only if action == KeyAction::Forward
|
|
|
|
// both for pressed and released, to avoid inconsistencies
|
2020-11-02 09:03:33 +00:00
|
|
|
matches!(action, KeyAction::Forward)
|
2018-05-08 10:47:09 +00:00
|
|
|
});
|
|
|
|
if let KeyState::Released = state {
|
|
|
|
// only process special actions on key press, not release
|
2021-05-30 22:56:37 +00:00
|
|
|
return KeyAction::None;
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
2021-06-09 20:46:09 +00:00
|
|
|
action
|
2020-06-11 17:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 11:03:36 +00:00
|
|
|
fn on_pointer_button<B: InputBackend>(&mut self, evt: B::PointerButtonEvent) {
|
2018-10-04 21:37:45 +00:00
|
|
|
let serial = SCOUNTER.next_serial();
|
2018-05-08 10:47:09 +00:00
|
|
|
let button = match evt.button() {
|
|
|
|
input::MouseButton::Left => 0x110,
|
|
|
|
input::MouseButton::Right => 0x111,
|
|
|
|
input::MouseButton::Middle => 0x112,
|
|
|
|
input::MouseButton::Other(b) => b as u32,
|
|
|
|
};
|
|
|
|
let state = match evt.state() {
|
|
|
|
input::MouseButtonState::Pressed => {
|
2018-10-04 15:40:03 +00:00
|
|
|
// change the keyboard focus unless the pointer is grabbed
|
|
|
|
if !self.pointer.is_grabbed() {
|
|
|
|
let under = self
|
|
|
|
.window_map
|
|
|
|
.borrow_mut()
|
2021-05-31 19:06:58 +00:00
|
|
|
.get_surface_and_bring_to_top(self.pointer_location);
|
2018-10-04 15:40:03 +00:00
|
|
|
self.keyboard
|
|
|
|
.set_focus(under.as_ref().map(|&(ref s, _)| s), serial);
|
|
|
|
}
|
2018-05-08 10:47:09 +00:00
|
|
|
wl_pointer::ButtonState::Pressed
|
|
|
|
}
|
|
|
|
input::MouseButtonState::Released => wl_pointer::ButtonState::Released,
|
|
|
|
};
|
|
|
|
self.pointer.button(button, state, serial, evt.time());
|
|
|
|
}
|
|
|
|
|
2020-05-04 11:03:36 +00:00
|
|
|
fn on_pointer_axis<B: InputBackend>(&mut self, evt: B::PointerAxisEvent) {
|
2018-05-08 10:47:09 +00:00
|
|
|
let source = match evt.source() {
|
|
|
|
input::AxisSource::Continuous => wl_pointer::AxisSource::Continuous,
|
|
|
|
input::AxisSource::Finger => wl_pointer::AxisSource::Finger,
|
|
|
|
input::AxisSource::Wheel | input::AxisSource::WheelTilt => wl_pointer::AxisSource::Wheel,
|
|
|
|
};
|
2018-09-24 22:32:09 +00:00
|
|
|
let horizontal_amount = evt
|
2020-01-01 10:43:16 +00:00
|
|
|
.amount(input::Axis::Horizontal)
|
|
|
|
.unwrap_or_else(|| evt.amount_discrete(input::Axis::Horizontal).unwrap() * 3.0);
|
2018-09-24 22:32:09 +00:00
|
|
|
let vertical_amount = evt
|
2020-01-01 10:43:16 +00:00
|
|
|
.amount(input::Axis::Vertical)
|
|
|
|
.unwrap_or_else(|| evt.amount_discrete(input::Axis::Vertical).unwrap() * 3.0);
|
|
|
|
let horizontal_amount_discrete = evt.amount_discrete(input::Axis::Horizontal);
|
|
|
|
let vertical_amount_discrete = evt.amount_discrete(input::Axis::Vertical);
|
2018-05-08 10:47:09 +00:00
|
|
|
|
|
|
|
{
|
2018-10-04 15:40:03 +00:00
|
|
|
let mut frame = AxisFrame::new(evt.time()).source(source);
|
2018-05-08 10:47:09 +00:00
|
|
|
if horizontal_amount != 0.0 {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.value(wl_pointer::Axis::HorizontalScroll, horizontal_amount);
|
2018-05-08 10:47:09 +00:00
|
|
|
if let Some(discrete) = horizontal_amount_discrete {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.discrete(wl_pointer::Axis::HorizontalScroll, discrete as i32);
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
|
|
|
} else if source == wl_pointer::AxisSource::Finger {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.stop(wl_pointer::Axis::HorizontalScroll);
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
|
|
|
if vertical_amount != 0.0 {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.value(wl_pointer::Axis::VerticalScroll, vertical_amount);
|
2018-05-08 10:47:09 +00:00
|
|
|
if let Some(discrete) = vertical_amount_discrete {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.discrete(wl_pointer::Axis::VerticalScroll, discrete as i32);
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
|
|
|
} else if source == wl_pointer::AxisSource::Finger {
|
2018-10-04 15:40:03 +00:00
|
|
|
frame = frame.stop(wl_pointer::Axis::VerticalScroll);
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
2018-10-04 15:40:03 +00:00
|
|
|
self.pointer.axis(frame);
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 22:56:37 +00:00
|
|
|
#[cfg(feature = "winit")]
|
|
|
|
impl AnvilState<WinitData> {
|
|
|
|
pub fn process_input_event<B: InputBackend>(&mut self, event: InputEvent<B>) {
|
|
|
|
match event {
|
|
|
|
InputEvent::Keyboard { event, .. } => match self.keyboard_key_to_action::<B>(event) {
|
2021-05-31 19:06:58 +00:00
|
|
|
KeyAction::None | KeyAction::Forward => {}
|
2021-05-30 22:56:37 +00:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action => {
|
|
|
|
warn!(self.log, "Key action {:?} unsupported on winit backend.", action);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
InputEvent::PointerMotionAbsolute { event, .. } => self.on_pointer_move_absolute::<B>(event),
|
|
|
|
InputEvent::PointerButton { event, .. } => self.on_pointer_button::<B>(event),
|
|
|
|
InputEvent::PointerAxis { event, .. } => self.on_pointer_axis::<B>(event),
|
|
|
|
_ => {
|
|
|
|
// other events are not handled in anvil (yet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_pointer_move_absolute<B: InputBackend>(&mut self, evt: B::PointerMotionAbsoluteEvent) {
|
|
|
|
// different cases depending on the context:
|
|
|
|
let (x, y) = evt.position();
|
2021-05-31 19:06:58 +00:00
|
|
|
self.pointer_location = (x, y);
|
2021-05-30 22:56:37 +00:00
|
|
|
let serial = SCOUNTER.next_serial();
|
|
|
|
let under = self.window_map.borrow().get_surface_under((x as f64, y as f64));
|
|
|
|
self.pointer.motion((x, y), under, serial, evt.time());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
impl AnvilState<UdevData> {
|
|
|
|
pub fn process_input_event<B: InputBackend>(&mut self, event: InputEvent<B>) {
|
|
|
|
match event {
|
|
|
|
InputEvent::Keyboard { event, .. } => match self.keyboard_key_to_action::<B>(event) {
|
2021-05-31 19:06:58 +00:00
|
|
|
KeyAction::None | KeyAction::Forward => {}
|
2021-05-30 22:56:37 +00:00
|
|
|
KeyAction::Quit => {
|
|
|
|
info!(self.log, "Quitting.");
|
|
|
|
self.running.store(false, Ordering::SeqCst);
|
|
|
|
}
|
2021-05-31 19:06:58 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
KeyAction::VtSwitch(vt) => {
|
|
|
|
info!(self.log, "Trying to switch to vt {}", vt);
|
|
|
|
if let Err(err) = self.backend_data.session.change_vt(vt) {
|
|
|
|
error!(self.log, "Error switching to vt {}: {}", vt, err);
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 22:56:37 +00:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 19:06:58 +00:00
|
|
|
KeyAction::Screen(num) => {
|
|
|
|
if let Some(output) = self.backend_data.output_map.get(num) {
|
|
|
|
let x = self
|
|
|
|
.backend_data
|
|
|
|
.output_map
|
|
|
|
.iter()
|
|
|
|
.take(num)
|
|
|
|
.fold(0, |acc, output| acc + output.size.0)
|
|
|
|
as f64
|
|
|
|
+ (output.size.0 as f64 / 2.0);
|
|
|
|
let y = output.size.1 as f64 / 2.0;
|
|
|
|
self.pointer_location = (x as f64, y as f64)
|
|
|
|
}
|
2021-05-30 22:56:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
InputEvent::PointerMotion { event, .. } => self.on_pointer_move::<B>(event),
|
|
|
|
InputEvent::PointerButton { event, .. } => self.on_pointer_button::<B>(event),
|
|
|
|
InputEvent::PointerAxis { event, .. } => self.on_pointer_axis::<B>(event),
|
|
|
|
_ => {
|
|
|
|
// other events are not handled in anvil (yet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_pointer_move<B: InputBackend>(&mut self, evt: B::PointerMotionEvent) {
|
|
|
|
let (x, y) = (evt.delta_x(), evt.delta_y());
|
|
|
|
let serial = SCOUNTER.next_serial();
|
2021-05-31 19:06:58 +00:00
|
|
|
self.pointer_location.0 += x as f64;
|
|
|
|
self.pointer_location.1 += y as f64;
|
2021-05-30 22:56:37 +00:00
|
|
|
|
|
|
|
// clamp to screen limits
|
|
|
|
// this event is never generated by winit
|
2021-05-31 19:06:58 +00:00
|
|
|
self.pointer_location = self.clamp_coords(self.pointer_location);
|
2021-05-30 22:56:37 +00:00
|
|
|
|
2021-05-31 19:06:58 +00:00
|
|
|
let under = self.window_map.borrow().get_surface_under(self.pointer_location);
|
|
|
|
self.pointer
|
|
|
|
.motion(self.pointer_location, under, serial, evt.time());
|
2021-05-30 22:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clamp_coords(&self, pos: (f64, f64)) -> (f64, f64) {
|
2021-06-09 20:46:09 +00:00
|
|
|
if self.backend_data.output_map.is_empty() {
|
2021-05-30 22:56:37 +00:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (mut x, mut y) = pos;
|
|
|
|
// max_x is the sum of the width of all outputs
|
2021-05-31 19:06:58 +00:00
|
|
|
let max_x = self
|
|
|
|
.backend_data
|
|
|
|
.output_map
|
|
|
|
.iter()
|
|
|
|
.fold(0u32, |acc, output| acc + output.size.0);
|
2021-05-30 22:56:37 +00:00
|
|
|
x = x.max(0.0).min(max_x as f64);
|
|
|
|
|
|
|
|
// max y depends on the current output
|
|
|
|
let max_y = self.current_output_size(x).1;
|
|
|
|
y = y.max(0.0).min(max_y as f64);
|
|
|
|
|
|
|
|
(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_output_idx(&self, x: f64) -> usize {
|
2021-05-31 19:06:58 +00:00
|
|
|
self.backend_data
|
|
|
|
.output_map
|
2021-05-30 22:56:37 +00:00
|
|
|
.iter()
|
|
|
|
// map each output to their x position
|
|
|
|
.scan(0u32, |acc, output| {
|
|
|
|
let curr_x = *acc;
|
|
|
|
*acc += output.size.0;
|
|
|
|
Some(curr_x)
|
|
|
|
})
|
|
|
|
// get an index
|
|
|
|
.enumerate()
|
|
|
|
// find the first one with a greater x
|
|
|
|
.find(|(_idx, x_pos)| *x_pos as f64 > x)
|
|
|
|
// the previous output is the one we are on
|
|
|
|
.map(|(idx, _)| idx - 1)
|
2021-05-31 19:06:58 +00:00
|
|
|
.unwrap_or(self.backend_data.output_map.len() - 1)
|
2021-05-30 22:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn current_output_size(&self, x: f64) -> (u32, u32) {
|
2021-05-31 19:06:58 +00:00
|
|
|
self.backend_data.output_map[self.current_output_idx(x)].size
|
2021-05-30 22:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 10:47:09 +00:00
|
|
|
/// Possible results of a keyboard action
|
2021-05-30 22:56:37 +00:00
|
|
|
#[derive(Debug)]
|
2018-05-08 10:47:09 +00:00
|
|
|
enum KeyAction {
|
|
|
|
/// Quit the compositor
|
|
|
|
Quit,
|
|
|
|
/// Trigger a vt-switch
|
|
|
|
VtSwitch(i32),
|
|
|
|
/// run a command
|
|
|
|
Run(String),
|
2020-06-11 17:46:29 +00:00
|
|
|
/// Switch the current screen
|
|
|
|
Screen(usize),
|
2018-05-08 10:47:09 +00:00
|
|
|
/// Forward the key to the client
|
|
|
|
Forward,
|
|
|
|
/// Do nothing more
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2020-01-01 10:43:16 +00:00
|
|
|
fn process_keyboard_shortcut(modifiers: ModifiersState, keysym: Keysym) -> KeyAction {
|
2018-06-27 12:04:29 +00:00
|
|
|
if modifiers.ctrl && modifiers.alt && keysym == xkb::KEY_BackSpace
|
2018-09-24 22:32:09 +00:00
|
|
|
|| modifiers.logo && keysym == xkb::KEY_q
|
|
|
|
{
|
|
|
|
// ctrl+alt+backspace = quit
|
|
|
|
// logo + q = quit
|
|
|
|
KeyAction::Quit
|
2021-04-28 22:31:49 +00:00
|
|
|
} else if (xkb::KEY_XF86Switch_VT_1..=xkb::KEY_XF86Switch_VT_12).contains(&keysym) {
|
2018-05-08 10:47:09 +00:00
|
|
|
// VTSwicth
|
|
|
|
KeyAction::VtSwitch((keysym - xkb::KEY_XF86Switch_VT_1 + 1) as i32)
|
|
|
|
} else if modifiers.logo && keysym == xkb::KEY_Return {
|
|
|
|
// run terminal
|
|
|
|
KeyAction::Run("weston-terminal".into())
|
2020-06-11 17:46:29 +00:00
|
|
|
} else if modifiers.logo && keysym >= xkb::KEY_1 && keysym <= xkb::KEY_9 {
|
|
|
|
KeyAction::Screen((keysym - xkb::KEY_1) as usize)
|
2018-05-08 10:47:09 +00:00
|
|
|
} else {
|
|
|
|
KeyAction::Forward
|
|
|
|
}
|
|
|
|
}
|