cosmic-comp/src/input/mod.rs

792 lines
38 KiB
Rust
Raw Normal View History

2021-12-21 18:57:09 +01:00
// SPDX-License-Identifier: GPL-3.0-only
2022-05-03 13:37:51 +02:00
use crate::{
config::Action,
shell::Workspace,
state::{Common, State},
};
2021-12-21 18:57:09 +01:00
use smithay::{
2022-03-28 23:45:30 +02:00
backend::input::{Device, DeviceCapability, InputBackend, InputEvent, KeyState},
2022-03-30 18:46:28 +02:00
desktop::{layer_map_for_output, Kind, Space, WindowSurfaceType},
2021-12-22 20:14:09 +01:00
reexports::wayland_server::{protocol::wl_surface::WlSurface, Display},
utils::{Logical, Point, Rectangle},
2021-12-21 18:57:09 +01:00
wayland::{
data_device::set_data_device_focus,
output::Output,
2022-05-03 13:37:51 +02:00
seat::{keysyms, CursorImageStatus, FilterResult, KeysymHandle, Seat, XkbConfig},
2021-12-22 20:14:09 +01:00
shell::wlr_layer::Layer as WlrLayer,
SERIAL_COUNTER,
2021-12-21 18:57:09 +01:00
},
};
use std::{cell::RefCell, collections::HashMap};
2022-05-03 13:37:51 +02:00
use xkbcommon::xkb::KEY_XF86Switch_VT_12;
2021-12-21 18:57:09 +01:00
pub struct ActiveOutput(pub RefCell<Output>);
2022-01-11 17:22:23 +01:00
pub struct SupressedKeys(RefCell<Vec<u32>>);
2021-12-22 20:14:09 +01:00
pub struct Devices(RefCell<HashMap<String, Vec<DeviceCapability>>>);
2022-01-11 17:22:23 +01:00
impl SupressedKeys {
fn new() -> SupressedKeys {
SupressedKeys(RefCell::new(Vec::new()))
}
fn add(&self, keysym: &KeysymHandle) {
self.0.borrow_mut().push(keysym.raw_code());
}
fn filter(&self, keysym: &KeysymHandle) -> bool {
let mut keys = self.0.borrow_mut();
if let Some(i) = keys.iter().position(|x| *x == keysym.raw_code()) {
keys.remove(i);
true
} else {
false
}
}
}
2021-12-22 20:14:09 +01:00
impl Devices {
fn new() -> Devices {
Devices(RefCell::new(HashMap::new()))
}
fn add_device<D: Device>(&self, device: &D) -> Vec<DeviceCapability> {
let id = device.id();
let mut map = self.0.borrow_mut();
let caps = [DeviceCapability::Keyboard, DeviceCapability::Pointer]
.iter()
.cloned()
.filter(|c| device.has_capability(*c))
.collect::<Vec<_>>();
let new_caps = caps
.iter()
.cloned()
.filter(|c| map.values().flatten().all(|has| *c != *has))
.collect::<Vec<_>>();
map.insert(id, caps);
new_caps
}
pub fn has_device<D: Device>(&self, device: &D) -> bool {
self.0.borrow().contains_key(&device.id())
}
fn remove_device<D: Device>(&self, device: &D) -> Vec<DeviceCapability> {
let id = device.id();
let mut map = self.0.borrow_mut();
map.remove(&id)
.unwrap_or(Vec::new())
.into_iter()
.filter(|c| map.values().flatten().all(|has| *c != *has))
.collect()
}
}
2021-12-21 18:57:09 +01:00
pub fn add_seat(display: &mut Display, name: String) -> Seat {
let (seat, _) = Seat::new(display, name, None);
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
userdata.insert_if_missing(|| Devices::new());
2022-01-11 17:22:23 +01:00
userdata.insert_if_missing(|| SupressedKeys::new());
2022-03-24 20:32:31 +01:00
userdata.insert_if_missing(|| RefCell::new(CursorImageStatus::Default));
2021-12-21 18:57:09 +01:00
seat
}
2022-01-18 17:26:03 +01:00
pub fn active_output(seat: &Seat, state: &Common) -> Output {
2021-12-21 18:57:09 +01:00
seat.user_data()
.get::<ActiveOutput>()
.map(|x| x.0.borrow().clone())
.unwrap_or_else(|| {
state
2022-03-24 20:32:31 +01:00
.shell
2021-12-21 18:57:09 +01:00
.outputs()
.next()
.cloned()
.expect("Backend has no outputs?")
})
}
2021-12-22 20:14:09 +01:00
pub fn set_active_output(seat: &Seat, output: &Output) {
if !seat
.user_data()
.insert_if_missing(|| ActiveOutput(RefCell::new(output.clone())))
{
*seat
.user_data()
.get::<ActiveOutput>()
.unwrap()
.0
.borrow_mut() = output.clone();
}
}
2022-04-25 12:35:55 +02:00
impl State {
2021-12-22 20:14:09 +01:00
pub fn process_input_event<B: InputBackend>(&mut self, event: InputEvent<B>) {
use smithay::backend::input::Event;
match event {
InputEvent::DeviceAdded { device } => {
2022-04-25 12:35:55 +02:00
let seat = &mut self.common.last_active_seat;
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
for cap in devices.add_device(&device) {
match cap {
DeviceCapability::Keyboard => {
let _ =
seat.add_keyboard(XkbConfig::default(), 200, 25, |seat, focus| {
set_data_device_focus(
seat,
focus.and_then(|s| s.as_ref().client()),
)
});
}
DeviceCapability::Pointer => {
let output = self
2022-04-25 12:35:55 +02:00
.common
2022-03-24 20:32:31 +01:00
.shell
2021-12-22 20:14:09 +01:00
.outputs()
.next()
.expect("Backend initialized without output")
.clone();
seat.user_data()
.insert_if_missing(|| ActiveOutput(RefCell::new(output)));
let owned_seat = seat.clone();
seat.add_pointer(move |status| {
*owned_seat
.user_data()
.get::<RefCell<CursorImageStatus>>()
.unwrap()
.borrow_mut() = status;
});
}
_ => {}
}
}
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
{
2022-04-25 12:35:55 +02:00
self.common.egui.debug_state.handle_device_added(&device);
self.common.egui.log_state.handle_device_added(&device);
2022-01-11 17:22:23 +01:00
}
2021-12-22 20:14:09 +01:00
}
InputEvent::DeviceRemoved { device } => {
2022-04-25 12:35:55 +02:00
for seat in &mut self.common.seats {
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
for cap in devices.remove_device(&device) {
match cap {
DeviceCapability::Keyboard => {
seat.remove_keyboard();
}
DeviceCapability::Pointer => {
seat.remove_pointer();
}
_ => {}
}
}
break;
}
}
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
{
2022-04-25 12:35:55 +02:00
self.common.egui.debug_state.handle_device_added(&device);
self.common.egui.log_state.handle_device_added(&device);
2022-01-11 17:22:23 +01:00
}
2021-12-22 20:14:09 +01:00
}
InputEvent::Keyboard { event, .. } => {
use smithay::backend::input::KeyboardKeyEvent;
let device = event.device();
2022-04-25 12:35:55 +02:00
for seat in self.common.seats.clone().iter() {
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
let keycode = event.key_code();
let state = event.state();
slog_scope::trace!("key"; "keycode" => keycode, "state" => format!("{:?}", state));
let serial = SERIAL_COUNTER.next_serial();
let time = Event::time(&event);
2022-03-30 23:24:29 +02:00
if let Some(action) = seat
.get_keyboard()
.unwrap()
.input(keycode, state, serial, time, |modifiers, handle| {
2022-03-16 20:01:34 +01:00
if state == KeyState::Released
&& userdata.get::<SupressedKeys>().unwrap().filter(&handle)
{
2022-03-30 23:24:29 +02:00
return FilterResult::Intercept(None);
2022-02-09 14:25:42 +01:00
}
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
{
2022-05-03 13:37:51 +02:00
if self.common.seats.iter().position(|x| x == seat).unwrap()
== 0
2022-04-25 12:35:55 +02:00
&& self.common.egui.active
2022-01-11 17:22:23 +01:00
{
2022-04-25 12:35:55 +02:00
if self.common.egui.debug_state.wants_keyboard() {
self.common.egui.debug_state.handle_keyboard(
2022-02-05 00:40:17 +01:00
&handle,
state == KeyState::Pressed,
modifiers.clone(),
);
2022-03-30 23:24:29 +02:00
userdata.get::<SupressedKeys>().unwrap().add(&handle);
return FilterResult::Intercept(None);
2022-02-05 00:40:17 +01:00
}
2022-04-25 12:35:55 +02:00
if self.common.egui.log_state.wants_keyboard() {
self.common.egui.log_state.handle_keyboard(
2022-02-05 00:40:17 +01:00
&handle,
state == KeyState::Pressed,
modifiers.clone(),
);
2022-03-30 23:24:29 +02:00
userdata.get::<SupressedKeys>().unwrap().add(&handle);
return FilterResult::Intercept(None);
2022-02-05 00:40:17 +01:00
}
2022-01-11 17:22:23 +01:00
}
2022-03-28 23:45:30 +02:00
}
2022-05-03 13:37:51 +02:00
if state == KeyState::Pressed
&& (keysyms::KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12)
.contains(&handle.modified_sym())
{
if let Err(err) = self.backend.kms().switch_vt(
(handle.modified_sym() - keysyms::KEY_XF86Switch_VT_1 + 1)
as i32,
) {
slog_scope::error!(
"Failed switching virtual terminal: {}",
err
);
2022-04-25 12:35:55 +02:00
}
userdata.get::<SupressedKeys>().unwrap().add(&handle);
return FilterResult::Intercept(None);
}
2022-03-28 23:45:30 +02:00
// here we can handle global shortcuts and the like
2022-05-03 13:37:51 +02:00
for (binding, action) in
self.common.config.static_conf.key_bindings.iter()
{
2022-03-28 23:45:30 +02:00
if state == KeyState::Pressed
&& binding.modifiers == *modifiers
&& handle.raw_syms().contains(&binding.key)
{
2022-03-30 23:24:29 +02:00
userdata.get::<SupressedKeys>().unwrap().add(&handle);
return FilterResult::Intercept(Some(action));
2022-03-28 23:45:30 +02:00
}
2022-01-11 17:22:23 +01:00
}
2021-12-22 20:14:09 +01:00
FilterResult::Forward
2022-03-30 23:24:29 +02:00
})
.flatten()
{
match action {
Action::Terminate => {
2022-04-25 12:35:55 +02:00
self.common.should_stop = true;
2022-03-30 23:24:29 +02:00
}
#[cfg(feature = "debug")]
Action::Debug => {
2022-04-25 12:35:55 +02:00
self.common.egui.active = !self.common.egui.active;
2022-03-30 23:24:29 +02:00
}
#[cfg(not(feature = "debug"))]
Action::Debug => {
slog_scope::info!("Debug overlay not included in this version")
}
Action::Close => {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
2022-05-03 13:37:51 +02:00
let workspace =
self.common.shell.active_space_mut(&current_output);
2022-03-30 23:24:29 +02:00
if let Some(window) = workspace.focus_stack(seat).last() {
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
xdg.send_close();
}
}
}
Action::Workspace(key_num) => {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
2022-03-30 23:24:29 +02:00
let workspace = match key_num {
0 => 9,
x => x - 1,
};
2022-05-03 13:37:51 +02:00
self.common.shell.activate(
seat,
&current_output,
workspace as usize,
);
2022-03-30 23:24:29 +02:00
}
Action::MoveToWorkspace(key_num) => {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
2022-03-30 23:24:29 +02:00
let workspace = match key_num {
0 => 9,
x => x - 1,
};
2022-04-25 12:35:55 +02:00
self.common.shell.move_current_window(
2022-03-30 23:24:29 +02:00
seat,
&current_output,
workspace as usize,
);
}
Action::Focus(focus) => {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
self.common.shell.move_focus(
2022-03-30 23:24:29 +02:00
seat,
&current_output,
*focus,
2022-04-25 12:35:55 +02:00
self.common.seats.iter(),
2022-03-30 23:24:29 +02:00
);
}
2022-04-22 15:18:28 +02:00
Action::Fullscreen => {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
2022-05-03 13:37:51 +02:00
let workspace =
self.common.shell.active_space_mut(&current_output);
2022-04-22 15:18:28 +02:00
let focused_window = workspace.focus_stack(seat).last();
if let Some(window) = focused_window {
workspace.fullscreen_toggle(&window, &current_output);
}
}
2022-03-30 23:24:29 +02:00
Action::Orientation(orientation) => {
2022-04-25 12:35:55 +02:00
let output = active_output(seat, &self.common);
2022-05-03 13:37:51 +02:00
self.common
.shell
.set_orientation(&seat, &output, *orientation);
2022-03-30 23:24:29 +02:00
}
Action::Spawn(command) => {
if let Err(err) = std::process::Command::new("/bin/sh")
.arg("-c")
.arg(command)
2022-04-25 12:35:55 +02:00
.env("WAYLAND_DISPLAY", &self.common.socket)
2022-03-30 23:24:29 +02:00
.spawn()
{
slog_scope::warn!("Failed to spawn: {}", err);
}
}
}
}
2021-12-22 20:14:09 +01:00
break;
}
}
}
InputEvent::PointerMotion { event, .. } => {
use smithay::backend::input::PointerMotionEvent;
let device = event.device();
2022-04-25 12:35:55 +02:00
for seat in self.common.seats.clone().iter() {
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
2022-04-25 12:35:55 +02:00
let current_output = active_output(seat, &self.common);
2021-12-22 20:14:09 +01:00
let mut position = seat.get_pointer().unwrap().current_location();
position += event.delta();
let output = self
2022-04-25 12:35:55 +02:00
.common
2022-03-24 20:32:31 +01:00
.shell
2021-12-22 20:14:09 +01:00
.outputs()
.find(|output| {
2022-04-25 12:35:55 +02:00
self.common
.shell
2021-12-22 20:14:09 +01:00
.output_geometry(output)
.to_f64()
.contains(position)
})
2021-12-28 16:23:12 +01:00
.cloned()
.unwrap_or(current_output.clone());
if output != current_output {
set_active_output(seat, &output);
2021-12-22 20:14:09 +01:00
}
2022-04-25 12:35:55 +02:00
let output_geometry = self.common.shell.output_geometry(&output);
2021-12-22 20:14:09 +01:00
position.x = 0.0f64
.max(position.x)
.min((output_geometry.loc.x + output_geometry.size.w) as f64);
position.y = 0.0f64
.max(position.y)
.min((output_geometry.loc.y + output_geometry.size.h) as f64);
let serial = SERIAL_COUNTER.next_serial();
2022-05-03 13:37:51 +02:00
let relative_pos = self
.common
.shell
.space_relative_output_geometry(position, &output);
2022-04-25 12:35:55 +02:00
let workspace = self.common.shell.active_space_mut(&output);
let under = State::surface_under(
position,
relative_pos,
&output,
output_geometry,
2022-04-22 15:18:28 +02:00
&workspace,
);
2022-03-24 20:32:31 +01:00
handle_window_movement(
under.as_ref().map(|(s, _)| s),
&mut workspace.space,
);
2021-12-22 20:14:09 +01:00
seat.get_pointer()
.unwrap()
.motion(position, under, serial, event.time());
2021-12-28 16:23:12 +01:00
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
2022-04-25 12:35:55 +02:00
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 {
2022-05-03 13:37:51 +02:00
self.common
.egui
2022-02-05 00:40:17 +01:00
.debug_state
.handle_pointer_motion(position.to_i32_round());
2022-05-03 13:37:51 +02:00
self.common
.egui
2022-02-05 00:40:17 +01:00
.log_state
2022-01-11 17:22:23 +01:00
.handle_pointer_motion(position.to_i32_round());
}
2021-12-22 20:14:09 +01:00
break;
}
}
}
InputEvent::PointerMotionAbsolute { event, .. } => {
use smithay::backend::input::PointerMotionAbsoluteEvent;
let device = event.device();
2022-04-25 12:35:55 +02:00
for seat in self.common.seats.clone().iter() {
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
2022-04-25 12:35:55 +02:00
let output = active_output(seat, &self.common);
let geometry = self.common.shell.output_geometry(&output);
2021-12-22 20:14:09 +01:00
let position =
geometry.loc.to_f64() + event.position_transformed(geometry.size);
2022-05-03 13:37:51 +02:00
let relative_pos = self
.common
.shell
.space_relative_output_geometry(position, &output);
2022-04-25 12:35:55 +02:00
let workspace = self.common.shell.active_space_mut(&output);
2021-12-22 20:14:09 +01:00
let serial = SERIAL_COUNTER.next_serial();
2022-04-25 12:35:55 +02:00
let under = State::surface_under(
position,
relative_pos,
&output,
geometry,
2022-04-22 15:18:28 +02:00
&workspace,
);
2022-03-24 20:32:31 +01:00
handle_window_movement(
under.as_ref().map(|(s, _)| s),
&mut workspace.space,
);
2021-12-22 20:14:09 +01:00
seat.get_pointer()
.unwrap()
.motion(position, under, serial, event.time());
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
2022-04-25 12:35:55 +02:00
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 {
2022-05-03 13:37:51 +02:00
self.common
.egui
2022-02-05 00:40:17 +01:00
.debug_state
.handle_pointer_motion(position.to_i32_round());
2022-05-03 13:37:51 +02:00
self.common
.egui
2022-02-05 00:40:17 +01:00
.log_state
2022-01-11 17:22:23 +01:00
.handle_pointer_motion(position.to_i32_round());
}
2021-12-22 20:14:09 +01:00
break;
}
}
}
InputEvent::PointerButton { event, .. } => {
use smithay::{
backend::input::{ButtonState, PointerButtonEvent},
reexports::wayland_server::protocol::wl_pointer,
};
let device = event.device();
2022-04-25 12:35:55 +02:00
for seat in self.common.seats.clone().iter() {
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
2022-04-25 12:35:55 +02:00
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0
&& self.common.egui.active
2022-01-11 17:22:23 +01:00
{
2022-04-25 12:35:55 +02:00
if self.common.egui.debug_state.wants_pointer() {
2022-02-05 00:40:17 +01:00
if let Some(button) = event.button() {
2022-04-25 12:35:55 +02:00
self.common.egui.debug_state.handle_pointer_button(
2022-02-05 00:40:17 +01:00
button,
event.state() == ButtonState::Pressed,
2022-04-25 12:35:55 +02:00
self.common.egui.modifiers.clone(),
2022-02-05 00:40:17 +01:00
);
}
break;
}
2022-04-25 12:35:55 +02:00
if self.common.egui.log_state.wants_pointer() {
2022-02-05 00:40:17 +01:00
if let Some(button) = event.button() {
2022-04-25 12:35:55 +02:00
self.common.egui.log_state.handle_pointer_button(
2022-02-05 00:40:17 +01:00
button,
event.state() == ButtonState::Pressed,
2022-04-25 12:35:55 +02:00
self.common.egui.modifiers.clone(),
2022-02-05 00:40:17 +01:00
);
}
break;
2022-01-11 17:22:23 +01:00
}
}
2021-12-22 20:14:09 +01:00
let serial = SERIAL_COUNTER.next_serial();
let button = event.button_code();
let state = match event.state() {
ButtonState::Pressed => {
// change the keyboard focus unless the pointer is grabbed
if !seat.get_pointer().unwrap().is_grabbed() {
2022-04-25 12:35:55 +02:00
let output = active_output(seat, &self.common);
2022-04-05 23:53:01 +02:00
let pos = seat.get_pointer().unwrap().current_location();
2022-04-25 12:35:55 +02:00
let output_geo = self.common.shell.output_geometry(&output);
2022-05-03 13:37:51 +02:00
let relative_pos = self
.common
.shell
.space_relative_output_geometry(pos, &output);
2022-04-25 12:35:55 +02:00
let workspace = self.common.shell.active_space_mut(&output);
2021-12-22 20:14:09 +01:00
let layers = layer_map_for_output(&output);
let mut under = None;
2022-04-22 15:18:28 +02:00
if let Some(window) = workspace.get_fullscreen(&output) {
2022-05-03 13:37:51 +02:00
if let Some(layer) =
layers.layer_under(WlrLayer::Overlay, relative_pos)
2022-04-22 15:18:28 +02:00
{
if layer.can_receive_keyboard_focus() {
let layer_loc =
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::TOPLEVEL,
2022-04-22 15:18:28 +02:00
)
.map(|(s, _)| s);
}
2022-05-03 13:37:51 +02:00
} else {
under = window
.surface_under(
pos - output_geo.loc.to_f64(),
WindowSurfaceType::TOPLEVEL,
2022-05-03 13:37:51 +02:00
)
.map(|(s, _)| s);
2022-04-22 15:18:28 +02:00
}
} else {
if let Some(layer) = layers
.layer_under(WlrLayer::Overlay, relative_pos)
2022-05-03 13:37:51 +02:00
.or_else(|| {
layers.layer_under(WlrLayer::Top, relative_pos)
})
2022-04-22 15:18:28 +02:00
{
if layer.can_receive_keyboard_focus() {
let layer_loc =
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::TOPLEVEL,
2022-04-22 15:18:28 +02:00
)
.map(|(s, _)| s);
}
} else if let Some((_, surface, _)) =
2022-05-03 13:37:51 +02:00
workspace.space.surface_under(
relative_pos,
WindowSurfaceType::TOPLEVEL,
2022-05-03 13:37:51 +02:00
)
2022-04-22 15:18:28 +02:00
{
under = Some(surface);
2022-05-03 13:37:51 +02:00
} else if let Some(layer) =
layers.layer_under(WlrLayer::Bottom, pos).or_else(
|| layers.layer_under(WlrLayer::Background, pos),
)
2022-04-22 15:18:28 +02:00
{
if layer.can_receive_keyboard_focus() {
let layer_loc =
layers.layer_geometry(layer).unwrap().loc;
under = layer
.surface_under(
pos - output_geo.loc.to_f64()
- layer_loc.to_f64(),
WindowSurfaceType::TOPLEVEL,
2022-04-22 15:18:28 +02:00
)
.map(|(s, _)| s);
}
};
}
2021-12-22 20:14:09 +01:00
2022-04-25 12:35:55 +02:00
self.common.set_focus(under.as_ref(), seat, Some(serial));
2021-12-22 20:14:09 +01:00
}
wl_pointer::ButtonState::Pressed
}
ButtonState::Released => wl_pointer::ButtonState::Released,
};
seat.get_pointer()
.unwrap()
.button(button, state, serial, event.time());
break;
}
}
}
InputEvent::PointerAxis { event, .. } => {
use smithay::{
backend::input::{Axis, AxisSource, PointerAxisEvent},
reexports::wayland_server::protocol::wl_pointer,
wayland::seat::AxisFrame,
};
let device = event.device();
2022-04-25 12:35:55 +02:00
for seat in self.common.seats.clone().iter() {
2022-01-11 17:22:23 +01:00
#[cfg(feature = "debug")]
2022-05-03 13:37:51 +02:00
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0
&& self.common.egui.active
{
2022-04-25 12:35:55 +02:00
if self.common.egui.debug_state.wants_pointer() {
self.common.egui.debug_state.handle_pointer_axis(
2022-02-05 00:40:17 +01:00
event
.amount_discrete(Axis::Horizontal)
.or_else(|| event.amount(Axis::Horizontal).map(|x| x * 3.0))
.unwrap_or(0.0),
event
.amount_discrete(Axis::Vertical)
.or_else(|| event.amount(Axis::Vertical).map(|x| x * 3.0))
.unwrap_or(0.0),
);
break;
}
2022-04-25 12:35:55 +02:00
if self.common.egui.log_state.wants_pointer() {
self.common.egui.log_state.handle_pointer_axis(
2022-02-05 00:40:17 +01:00
event
.amount_discrete(Axis::Horizontal)
.or_else(|| event.amount(Axis::Horizontal).map(|x| x * 3.0))
.unwrap_or(0.0),
event
.amount_discrete(Axis::Vertical)
.or_else(|| event.amount(Axis::Vertical).map(|x| x * 3.0))
.unwrap_or(0.0),
);
break;
}
2022-01-11 17:22:23 +01:00
}
2021-12-22 20:14:09 +01:00
let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) {
let source = match event.source() {
AxisSource::Continuous => wl_pointer::AxisSource::Continuous,
AxisSource::Finger => wl_pointer::AxisSource::Finger,
AxisSource::Wheel | AxisSource::WheelTilt => {
wl_pointer::AxisSource::Wheel
}
};
let horizontal_amount =
event.amount(Axis::Horizontal).unwrap_or_else(|| {
event.amount_discrete(Axis::Horizontal).unwrap() * 3.0
});
let vertical_amount = event.amount(Axis::Vertical).unwrap_or_else(|| {
event.amount_discrete(Axis::Vertical).unwrap() * 3.0
});
let horizontal_amount_discrete = event.amount_discrete(Axis::Horizontal);
let vertical_amount_discrete = event.amount_discrete(Axis::Vertical);
{
let mut frame = AxisFrame::new(event.time()).source(source);
if horizontal_amount != 0.0 {
frame = frame
.value(wl_pointer::Axis::HorizontalScroll, horizontal_amount);
if let Some(discrete) = horizontal_amount_discrete {
frame = frame.discrete(
wl_pointer::Axis::HorizontalScroll,
discrete as i32,
);
}
} else if source == wl_pointer::AxisSource::Finger {
frame = frame.stop(wl_pointer::Axis::HorizontalScroll);
}
if vertical_amount != 0.0 {
frame =
frame.value(wl_pointer::Axis::VerticalScroll, vertical_amount);
if let Some(discrete) = vertical_amount_discrete {
frame = frame.discrete(
wl_pointer::Axis::VerticalScroll,
discrete as i32,
);
}
} else if source == wl_pointer::AxisSource::Finger {
frame = frame.stop(wl_pointer::Axis::VerticalScroll);
}
seat.get_pointer().unwrap().axis(frame);
}
break;
}
}
}
_ => { /* TODO e.g. tablet or touch events */ }
}
}
pub fn surface_under(
global_pos: Point<f64, Logical>,
relative_pos: Point<f64, Logical>,
2021-12-22 20:14:09 +01:00
output: &Output,
output_geo: Rectangle<i32, Logical>,
2022-04-22 15:18:28 +02:00
workspace: &Workspace,
2021-12-22 20:14:09 +01:00
) -> Option<(WlSurface, Point<i32, Logical>)> {
let layers = layer_map_for_output(output);
2022-04-22 15:18:28 +02:00
if let Some(window) = workspace.get_fullscreen(output) {
if let Some(layer) = layers
.layer_under(WlrLayer::Overlay, relative_pos)
.or_else(|| layers.layer_under(WlrLayer::Top, relative_pos))
{
let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer
.surface_under(
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
2022-04-22 15:18:28 +02:00
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc))
2022-05-03 13:37:51 +02:00
} else {
2022-04-22 15:18:28 +02:00
window
.surface_under(global_pos - output_geo.loc.to_f64(), WindowSurfaceType::ALL)
2022-05-03 13:37:51 +02:00
.map(|(s, loc)| (s, loc + output_geo.loc))
2022-04-22 15:18:28 +02:00
}
2021-12-22 20:14:09 +01:00
} else {
2022-04-22 15:18:28 +02:00
if let Some(layer) = layers
.layer_under(WlrLayer::Overlay, relative_pos)
.or_else(|| layers.layer_under(WlrLayer::Top, relative_pos))
{
let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer
.surface_under(
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc))
2022-05-03 13:37:51 +02:00
} else if let Some((_, surface, loc)) = workspace
.space
.surface_under(relative_pos, WindowSurfaceType::ALL)
{
Some((surface, loc + (global_pos - relative_pos).to_i32_round()))
2022-04-22 15:18:28 +02:00
} else if let Some(layer) = layers
.layer_under(WlrLayer::Bottom, relative_pos)
.or_else(|| layers.layer_under(WlrLayer::Background, relative_pos))
{
let layer_loc = layers.layer_geometry(layer).unwrap().loc;
layer
.surface_under(
global_pos - output_geo.loc.to_f64() - layer_loc.to_f64(),
WindowSurfaceType::ALL,
)
.map(|(s, loc)| (s, loc + layer_loc + output_geo.loc))
} else {
None
}
2021-12-22 20:14:09 +01:00
}
}
}
2021-12-28 16:23:12 +01:00
pub fn handle_window_movement(surface: Option<&WlSurface>, space: &mut Space) {
2022-03-24 20:32:31 +01:00
// TODO: this is why to hardcoded and hacky, but wayland-rs 0.30 will make this unnecessary anyway.
2021-12-28 16:23:12 +01:00
if let Some(surface) = surface {
if let Some(window) = space.window_for_surface(&surface).cloned() {
if let Some(new_position) =
2022-03-24 20:32:31 +01:00
crate::shell::layout::floating::MoveSurfaceGrab::apply_move_state(&window)
2021-12-28 16:23:12 +01:00
{
2022-01-06 19:15:22 +01:00
space.map_window(&window, new_position, true);
2021-12-28 16:23:12 +01:00
}
}
}
}