input: Only use common state

This commit is contained in:
Victoria Brekenfeld 2022-01-18 17:26:03 +01:00
parent c413bb8052
commit ef9e3d3c7c
3 changed files with 53 additions and 65 deletions

View file

@ -101,7 +101,6 @@ impl X11State {
{ {
if let Err(err) = surface.render_output( if let Err(err) = surface.render_output(
&mut *x11_state.renderer.borrow_mut(), &mut *x11_state.renderer.borrow_mut(),
&output_ref,
&mut state.common, &mut state.common,
) { ) {
slog_scope::error!("Error rendering: {}", err); slog_scope::error!("Error rendering: {}", err);
@ -140,7 +139,6 @@ impl Surface {
pub fn render_output( pub fn render_output(
&mut self, &mut self,
renderer: &mut Gles2Renderer, renderer: &mut Gles2Renderer,
output: &Output,
state: &mut Common, state: &mut Common,
) -> Result<()> { ) -> Result<()> {
#[allow(unused_mut)] #[allow(unused_mut)]
@ -148,7 +146,7 @@ impl Surface {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
let space = state.spaces.active_space(output); let space = state.spaces.active_space(&self.output);
let size = space.output_geometry(&self.output).unwrap(); let size = space.output_geometry(&self.output).unwrap();
let scale = space.output_scale(&self.output).unwrap(); let scale = space.output_scale(&self.output).unwrap();
let frame = debug_ui(state, &self.fps, size, scale, true); let frame = debug_ui(state, &self.fps, size, scale, true);
@ -157,7 +155,7 @@ impl Surface {
); );
} }
let space = state.spaces.active_space_mut(output); let space = state.spaces.active_space_mut(&self.output);
let (buffer, age) = self let (buffer, age) = self
.surface .surface
.buffer() .buffer()
@ -347,6 +345,6 @@ impl State {
_ => {} _ => {}
}; };
self.process_input_event(event); self.common.process_input_event(event);
} }
} }

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::state::State; use crate::state::Common;
use smithay::{ use smithay::{
backend::input::{Device, DeviceCapability, InputBackend, InputEvent}, backend::input::{Device, DeviceCapability, InputBackend, InputEvent},
desktop::{layer_map_for_output, Space}, desktop::{layer_map_for_output, Space},
@ -91,13 +91,12 @@ pub fn add_seat(display: &mut Display, name: String) -> Seat {
seat seat
} }
pub fn active_output(seat: &Seat, state: &State) -> Output { pub fn active_output(seat: &Seat, state: &Common) -> Output {
seat.user_data() seat.user_data()
.get::<ActiveOutput>() .get::<ActiveOutput>()
.map(|x| x.0.borrow().clone()) .map(|x| x.0.borrow().clone())
.unwrap_or_else(|| { .unwrap_or_else(|| {
state state
.common
.spaces .spaces
.outputs() .outputs()
.next() .next()
@ -120,13 +119,13 @@ pub fn set_active_output(seat: &Seat, output: &Output) {
} }
} }
impl State { impl Common {
pub fn process_input_event<B: InputBackend>(&mut self, event: InputEvent<B>) { pub fn process_input_event<B: InputBackend>(&mut self, event: InputEvent<B>) {
use smithay::backend::input::Event; use smithay::backend::input::Event;
match event { match event {
InputEvent::DeviceAdded { device } => { InputEvent::DeviceAdded { device } => {
let seat = &mut self.common.last_active_seat; let seat = &mut self.last_active_seat;
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
for cap in devices.add_device(&device) { for cap in devices.add_device(&device) {
@ -142,7 +141,6 @@ impl State {
} }
DeviceCapability::Pointer => { DeviceCapability::Pointer => {
let output = self let output = self
.common
.spaces .spaces
.outputs() .outputs()
.next() .next()
@ -168,7 +166,7 @@ impl State {
} }
} }
InputEvent::DeviceRemoved { device } => { InputEvent::DeviceRemoved { device } => {
for seat in &mut self.common.seats { for seat in &mut self.seats {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -195,7 +193,7 @@ impl State {
use smithay::backend::input::KeyboardKeyEvent; use smithay::backend::input::KeyboardKeyEvent;
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -218,23 +216,23 @@ impl State {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
self.common.egui.modifiers = modifiers.clone(); self.egui.modifiers = modifiers.clone();
if self.common.seats.iter().position(|x| x == seat).unwrap() if self.seats.iter().position(|x| x == seat).unwrap()
== 0 == 0
&& modifiers.logo && modifiers.logo
&& handle.raw_syms().contains(&keysyms::KEY_Escape) && handle.raw_syms().contains(&keysyms::KEY_Escape)
&& state == KeyState::Pressed && state == KeyState::Pressed
{ {
self.common.egui.active = !self.common.egui.active; self.egui.active = !self.egui.active;
userdata.get::<SupressedKeys>().unwrap().add(&handle); userdata.get::<SupressedKeys>().unwrap().add(&handle);
return FilterResult::Intercept(()); return FilterResult::Intercept(());
} }
if self.common.seats.iter().position(|x| x == seat).unwrap() if self.seats.iter().position(|x| x == seat).unwrap()
== 0 == 0
&& self.common.egui.active && self.egui.active
&& self.common.egui.state.wants_keyboard() && self.egui.state.wants_keyboard()
{ {
self.common.egui.state.handle_keyboard( self.egui.state.handle_keyboard(
&handle, &handle,
state == KeyState::Pressed, state == KeyState::Pressed,
modifiers.clone(), modifiers.clone(),
@ -255,7 +253,7 @@ impl State {
use smithay::backend::input::PointerMotionEvent; use smithay::backend::input::PointerMotionEvent;
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
@ -265,12 +263,10 @@ impl State {
position += event.delta(); position += event.delta();
let output = self let output = self
.common
.spaces .spaces
.outputs() .outputs()
.find(|output| { .find(|output| {
self.common self.spaces
.spaces
.output_geometry(output) .output_geometry(output)
.to_f64() .to_f64()
.contains(position) .contains(position)
@ -280,7 +276,7 @@ impl State {
if output != current_output { if output != current_output {
set_active_output(seat, &output); set_active_output(seat, &output);
} }
let output_geometry = self.common.spaces.output_geometry(&output); let output_geometry = self.spaces.output_geometry(&output);
position.x = 0.0f64 position.x = 0.0f64
.max(position.x) .max(position.x)
@ -290,17 +286,16 @@ impl State {
.min((output_geometry.loc.y + output_geometry.size.h) as f64); .min((output_geometry.loc.y + output_geometry.size.h) as f64);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
let space = self.common.spaces.active_space_mut(&output); let space = self.spaces.active_space_mut(&output);
let under = State::surface_under(position, &output, space); let under = Common::surface_under(position, &output, space);
handle_window_movement(under.as_ref().map(|(s, _)| s), space); handle_window_movement(under.as_ref().map(|(s, _)| s), space);
seat.get_pointer() seat.get_pointer()
.unwrap() .unwrap()
.motion(position, under, serial, event.time()); .motion(position, under, serial, event.time());
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 { if self.seats.iter().position(|x| x == seat).unwrap() == 0 {
self.common self.egui
.egui
.state .state
.handle_pointer_motion(position.to_i32_round()); .handle_pointer_motion(position.to_i32_round());
} }
@ -312,26 +307,25 @@ impl State {
use smithay::backend::input::PointerMotionAbsoluteEvent; use smithay::backend::input::PointerMotionAbsoluteEvent;
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
let output = active_output(seat, &self); let output = active_output(seat, &self);
let geometry = self.common.spaces.output_geometry(&output); let geometry = self.spaces.output_geometry(&output);
let space = self.common.spaces.active_space_mut(&output); let space = self.spaces.active_space_mut(&output);
let position = let position =
geometry.loc.to_f64() + event.position_transformed(geometry.size); geometry.loc.to_f64() + event.position_transformed(geometry.size);
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
let under = State::surface_under(position, &output, space); let under = Common::surface_under(position, &output, space);
handle_window_movement(under.as_ref().map(|(s, _)| s), space); handle_window_movement(under.as_ref().map(|(s, _)| s), space);
seat.get_pointer() seat.get_pointer()
.unwrap() .unwrap()
.motion(position, under, serial, event.time()); .motion(position, under, serial, event.time());
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 { if self.seats.iter().position(|x| x == seat).unwrap() == 0 {
self.common self.egui
.egui
.state .state
.handle_pointer_motion(position.to_i32_round()); .handle_pointer_motion(position.to_i32_round());
} }
@ -346,20 +340,20 @@ impl State {
}; };
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.seats.clone().iter() {
let userdata = seat.user_data(); let userdata = seat.user_data();
let devices = userdata.get::<Devices>().unwrap(); let devices = userdata.get::<Devices>().unwrap();
if devices.has_device(&device) { if devices.has_device(&device) {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 if self.seats.iter().position(|x| x == seat).unwrap() == 0
&& self.common.egui.active && self.egui.active
&& self.common.egui.state.wants_pointer() && self.egui.state.wants_pointer()
{ {
if let Some(button) = event.button() { if let Some(button) = event.button() {
self.common.egui.state.handle_pointer_button( self.egui.state.handle_pointer_button(
button, button,
event.state() == ButtonState::Pressed, event.state() == ButtonState::Pressed,
self.common.egui.modifiers.clone(), self.egui.modifiers.clone(),
); );
} }
break; break;
@ -373,8 +367,8 @@ impl State {
if !seat.get_pointer().unwrap().is_grabbed() { if !seat.get_pointer().unwrap().is_grabbed() {
let output = active_output(seat, &self); let output = active_output(seat, &self);
let mut pos = seat.get_pointer().unwrap().current_location(); let mut pos = seat.get_pointer().unwrap().current_location();
let output_geo = self.common.spaces.output_geometry(&output); let output_geo = self.spaces.output_geometry(&output);
let space = self.common.spaces.active_space_mut(&output); let space = self.spaces.active_space_mut(&output);
let layers = layer_map_for_output(&output); let layers = layer_map_for_output(&output);
pos -= output_geo.loc.to_f64(); pos -= output_geo.loc.to_f64();
let mut under = None; let mut under = None;
@ -433,13 +427,13 @@ impl State {
}; };
let device = event.device(); let device = event.device();
for seat in self.common.seats.clone().iter() { for seat in self.seats.clone().iter() {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
if self.common.seats.iter().position(|x| x == seat).unwrap() == 0 if self.seats.iter().position(|x| x == seat).unwrap() == 0
&& self.common.egui.active && self.egui.active
&& self.common.egui.state.wants_pointer() && self.egui.state.wants_pointer()
{ {
self.common.egui.state.handle_pointer_axis( self.egui.state.handle_pointer_axis(
event event
.amount_discrete(Axis::Horizontal) .amount_discrete(Axis::Horizontal)
.or_else(|| event.amount(Axis::Horizontal).map(|x| x * 3.0)) .or_else(|| event.amount(Axis::Horizontal).map(|x| x * 3.0))

View file

@ -51,22 +51,21 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
let (xdg_shell_state, _xdg_global) = xdg_shell_init( let (xdg_shell_state, _xdg_global) = xdg_shell_init(
display, display,
|event, mut ddata| { |event, mut ddata| {
let state = ddata.get::<State>().unwrap(); let state = &mut ddata.get::<State>().unwrap().common;
match event { match event {
XdgRequest::NewToplevel { surface } => { XdgRequest::NewToplevel { surface } => {
state.common.pending_toplevels.push(surface.clone()); state.pending_toplevels.push(surface.clone());
let seat = &state.common.last_active_seat; let seat = &state.last_active_seat;
let output = active_output(seat, &state); let output = active_output(seat, &*state);
let space = state.common.spaces.active_space_mut(&output); let space = state.spaces.active_space_mut(&output);
let window = Window::new(Kind::Xdg(surface)); let window = Window::new(Kind::Xdg(surface));
space.map_window(&window, (0, 0), true); space.map_window(&window, (0, 0), true);
// We will position the window after the first commit, when we know its size // We will position the window after the first commit, when we know its size
} }
XdgRequest::NewPopup { surface, .. } => { XdgRequest::NewPopup { surface, .. } => {
state state
.common
.shell .shell
.popups .popups
.track_popup(PopupKind::from(surface)) .track_popup(PopupKind::from(surface))
@ -101,7 +100,6 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
check_grab_preconditions(&seat, surface.get_surface(), serial) check_grab_preconditions(&seat, surface.get_surface(), serial)
{ {
let space = state let space = state
.common
.spaces .spaces
.space_for_surface(surface.get_surface().unwrap()) .space_for_surface(surface.get_surface().unwrap())
.unwrap(); .unwrap();
@ -164,7 +162,6 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
check_grab_preconditions(&seat, surface.get_surface(), serial) check_grab_preconditions(&seat, surface.get_surface(), serial)
{ {
let space = state let space = state
.common
.spaces .spaces
.space_for_surface(surface.get_surface().unwrap()) .space_for_surface(surface.get_surface().unwrap())
.unwrap(); .unwrap();
@ -185,7 +182,6 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
configure: Configure::Toplevel(configure), configure: Configure::Toplevel(configure),
} => { } => {
if let Some(window) = state if let Some(window) = state
.common
.spaces .spaces
.space_for_surface(&surface) .space_for_surface(&surface)
.and_then(|space| space.window_for_surface(&surface)) .and_then(|space| space.window_for_surface(&surface))
@ -194,9 +190,9 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
} }
} }
XdgRequest::Maximize { surface } => { XdgRequest::Maximize { surface } => {
let seat = &state.common.last_active_seat; let seat = &state.last_active_seat;
let output = active_output(seat, &state); let output = active_output(seat, &*state);
let space = state.common.spaces.active_space_mut(&output); let space = state.spaces.active_space_mut(&output);
let window = space let window = space
.window_for_surface(surface.get_surface().unwrap()) .window_for_surface(surface.get_surface().unwrap())
.unwrap() .unwrap()
@ -239,12 +235,12 @@ pub fn init_shell(display: &mut Display) -> ShellStates {
namespace, namespace,
.. ..
} => { } => {
let state = ddata.get::<State>().unwrap(); let state = &mut ddata.get::<State>().unwrap().common;
let seat = &state.common.last_active_seat; let seat = &state.last_active_seat;
let output = wl_output let output = wl_output
.as_ref() .as_ref()
.and_then(Output::from_resource) .and_then(Output::from_resource)
.unwrap_or_else(|| active_output(seat, &state)); .unwrap_or_else(|| active_output(seat, &*state));
let mut map = layer_map_for_output(&output); let mut map = layer_map_for_output(&output);
map.map_layer(&LayerSurface::new(surface, namespace)) map.map_layer(&LayerSurface::new(surface, namespace))