Dedup and clean up code

This commit is contained in:
Ottatop 2023-08-08 10:55:06 -05:00
parent 7123ecf8ba
commit c153eb6abb
2 changed files with 123 additions and 207 deletions

View file

@ -303,8 +303,18 @@ pub fn run_udev() -> Result<(), Box<dyn Error>> {
libinput_context.suspend(); libinput_context.suspend();
tracing::info!("pausing session"); tracing::info!("pausing session");
for backend in data.state.backend_data.backends.values() { for backend in data.state.backend_data.backends.values_mut() {
backend.drm.pause(); backend.drm.pause();
for surface in backend.surfaces.values_mut() {
if let Err(err) = surface.compositor.surface().reset_state() {
tracing::warn!("Failed to reset drm surface state: {}", err);
}
// reset the buffers after resume to trigger a full redraw
// this is important after a vt switch as the primary plane
// has no content and damage tracking may prevent a redraw
// otherwise
surface.compositor.reset_buffers();
}
} }
} }
session::Event::ActivateSession => { session::Event::ActivateSession => {
@ -1076,9 +1086,7 @@ impl State<UdevData> {
} }
fn device_removed(&mut self, node: DrmNode) { fn device_removed(&mut self, node: DrmNode) {
let device = if let Some(device) = self.backend_data.backends.get_mut(&node) { let Some(device) = self.backend_data.backends.get_mut(&node) else {
device
} else {
return; return;
}; };
@ -1291,15 +1299,11 @@ impl State<UdevData> {
} }
fn render_surface(&mut self, node: DrmNode, crtc: crtc::Handle) { fn render_surface(&mut self, node: DrmNode, crtc: crtc::Handle) {
let device = if let Some(device) = self.backend_data.backends.get_mut(&node) { let Some(device) = self.backend_data.backends.get_mut(&node) else {
device
} else {
return; return;
}; };
let surface = if let Some(surface) = device.surfaces.get_mut(&crtc) { let Some(surface) = device.surfaces.get_mut(&crtc) else {
surface
} else {
return; return;
}; };
@ -1584,7 +1588,7 @@ fn render_surface<'a>(
let time = clock.now(); let time = clock.now();
// We need to send frames to the cursor surface so that xwayland windows will properly // We need to send frames to the cursor surface so that xwayland windows will properly
// update on motion. // update the cursor on motion.
if let CursorImageStatus::Surface(surf) = cursor_status { if let CursorImageStatus::Surface(surf) = cursor_status {
send_frames_surface_tree(surf, output, time, Some(Duration::ZERO), |_, _| None); send_frames_surface_tree(surf, output, time, Some(Duration::ZERO), |_, _| None);
} }

View file

@ -8,10 +8,13 @@ use crate::{
window::WindowElement, window::WindowElement,
}; };
use smithay::{ use smithay::{
backend::{input::{ backend::{
input::{
AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent, AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent,
KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionEvent,
}, session::Session}, },
session::Session,
},
desktop::{layer_map_for_output, space::SpaceElement}, desktop::{layer_map_for_output, space::SpaceElement},
input::{ input::{
keyboard::{keysyms, FilterResult}, keyboard::{keysyms, FilterResult},
@ -87,6 +90,107 @@ impl<B: Backend> State<B> {
}) })
} }
fn keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) {
let serial = SERIAL_COUNTER.next_serial();
let time = event.time_msec();
let press_state = event.state();
let mut move_mode = false;
let action = self
.seat
.get_keyboard()
.expect("Seat has no keyboard") // FIXME: handle err
.input(
self,
event.key_code(),
press_state,
serial,
time,
|state, modifiers, keysym| {
if press_state == KeyState::Pressed {
let mut modifier_mask = Vec::<Modifier>::new();
if modifiers.alt {
modifier_mask.push(Modifier::Alt);
}
if modifiers.shift {
modifier_mask.push(Modifier::Shift);
}
if modifiers.ctrl {
modifier_mask.push(Modifier::Ctrl);
}
if modifiers.logo {
modifier_mask.push(Modifier::Super);
}
let raw_sym = if keysym.raw_syms().len() == 1 {
keysym.raw_syms()[0]
} else {
keysyms::KEY_NoSymbol
};
if let Some(callback_id) = state
.input_state
.keybinds
.get(&(modifier_mask.into(), raw_sym))
{
return FilterResult::Intercept(*callback_id);
} else if modifiers.ctrl
&& modifiers.shift
&& modifiers.alt
&& keysym.modified_sym() == keysyms::KEY_Escape
{
return FilterResult::Intercept(CallbackId(999999));
} else if let mut vt @ keysyms::KEY_XF86Switch_VT_1..=keysyms::KEY_XF86Switch_VT_12 =
keysym.modified_sym() {
vt = vt - keysyms::KEY_XF86Switch_VT_1 + 1;
tracing::info!("Switching to vt {vt}");
return FilterResult::Intercept(CallbackId(1000000 + vt));
}
}
if keysym.modified_sym() == keysyms::KEY_Control_L {
match press_state {
KeyState::Pressed => {
move_mode = true;
}
KeyState::Released => {
move_mode = false;
}
}
}
FilterResult::Forward
},
);
self.move_mode = move_mode;
if let Some(callback_id) = action {
if callback_id.0 == 999999 {
self.loop_signal.stop();
return;
}
if callback_id.0 > 1000000 {
let vt = callback_id.0 - 1000000;
if let Some(st) = (self as &mut dyn std::any::Any).downcast_mut::<State<UdevData>>()
{
if let Err(err) = st.backend_data.session.change_vt(vt as i32) {
tracing::error!("Failed to switch to vt {vt}: {err}");
}
}
return;
}
if let Some(stream) = self.api_state.stream.as_ref() {
if let Err(err) = crate::api::send_to_client(
&mut stream.lock().expect("Could not lock stream mutex"),
&OutgoingMsg::CallCallback {
callback_id,
args: None,
},
) {
tracing::warn!("error sending msg to client: {err}");
}
}
}
}
fn pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) { fn pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
let pointer = self.seat.get_pointer().expect("Seat has no pointer"); // FIXME: handle err let pointer = self.seat.get_pointer().expect("Seat has no pointer"); // FIXME: handle err
let keyboard = self.seat.get_keyboard().expect("Seat has no keyboard"); // FIXME: handle err let keyboard = self.seat.get_keyboard().expect("Seat has no keyboard"); // FIXME: handle err
@ -303,8 +407,6 @@ impl<B: Backend> State<B> {
.expect("Seat has no pointer") .expect("Seat has no pointer")
.axis(self, frame); .axis(self, frame);
} }
} }
impl State<WinitData> { impl State<WinitData> {
@ -324,97 +426,6 @@ impl State<WinitData> {
} }
} }
fn keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) {
let serial = SERIAL_COUNTER.next_serial();
let time = event.time_msec();
let press_state = event.state();
let mut move_mode = false;
let action = self
.seat
.get_keyboard()
.expect("Seat has no keyboard") // FIXME: handle err
.input(
self,
event.key_code(),
press_state,
serial,
time,
|state, modifiers, keysym| {
if press_state == KeyState::Pressed {
let mut modifier_mask = Vec::<Modifier>::new();
if modifiers.alt {
modifier_mask.push(Modifier::Alt);
}
if modifiers.shift {
modifier_mask.push(Modifier::Shift);
}
if modifiers.ctrl {
modifier_mask.push(Modifier::Ctrl);
}
if modifiers.logo {
modifier_mask.push(Modifier::Super);
}
let raw_sym = if keysym.raw_syms().len() == 1 {
keysym.raw_syms()[0]
} else {
keysyms::KEY_NoSymbol
};
if let Some(callback_id) = state
.input_state
.keybinds
.get(&(modifier_mask.into(), raw_sym))
{
return FilterResult::Intercept(*callback_id);
} else if modifiers.ctrl
&& modifiers.shift
&& modifiers.alt
&& keysym.modified_sym() == keysyms::KEY_Escape
{
return FilterResult::Intercept(CallbackId(999999));
} else if modifiers.ctrl && modifiers.alt {
if let mut vt @ keysyms::KEY_XF86Switch_VT_1..=keysyms::KEY_XF86Switch_VT_12 =
keysym.modified_sym() {
vt = vt - keysyms::KEY_XF86Switch_VT_1 + 1;
tracing::info!("Switching to vt {vt}");
}
}
}
if keysym.modified_sym() == keysyms::KEY_Control_L {
match press_state {
KeyState::Pressed => {
move_mode = true;
}
KeyState::Released => {
move_mode = false;
}
}
}
FilterResult::Forward
},
);
self.move_mode = move_mode;
if let Some(callback_id) = action {
if callback_id.0 == 999999 {
self.loop_signal.stop();
}
if let Some(stream) = self.api_state.stream.as_ref() {
if let Err(err) = crate::api::send_to_client(
&mut stream.lock().expect("Could not lock stream mutex"),
&OutgoingMsg::CallCallback {
callback_id,
args: None,
},
) {
tracing::warn!("error sending msg to client: {err}");
}
}
}
}
fn pointer_motion_absolute<I: InputBackend>(&mut self, event: I::PointerMotionAbsoluteEvent) { fn pointer_motion_absolute<I: InputBackend>(&mut self, event: I::PointerMotionAbsoluteEvent) {
let Some(output) = self.space.outputs().next() else { return; }; let Some(output) = self.space.outputs().next() else { return; };
let output_geo = self let output_geo = self
@ -479,105 +490,6 @@ impl State<UdevData> {
} }
} }
fn keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) {
let serial = SERIAL_COUNTER.next_serial();
let time = event.time_msec();
let press_state = event.state();
let mut move_mode = false;
let action = self
.seat
.get_keyboard()
.expect("Seat has no keyboard") // FIXME: handle err
.input(
self,
event.key_code(),
press_state,
serial,
time,
|state, modifiers, keysym| {
if press_state == KeyState::Pressed {
let mut modifier_mask = Vec::<Modifier>::new();
if modifiers.alt {
modifier_mask.push(Modifier::Alt);
}
if modifiers.shift {
modifier_mask.push(Modifier::Shift);
}
if modifiers.ctrl {
modifier_mask.push(Modifier::Ctrl);
}
if modifiers.logo {
modifier_mask.push(Modifier::Super);
}
let raw_sym = if keysym.raw_syms().len() == 1 {
keysym.raw_syms()[0]
} else {
keysyms::KEY_NoSymbol
};
if let Some(callback_id) = state
.input_state
.keybinds
.get(&(modifier_mask.into(), raw_sym))
{
return FilterResult::Intercept(*callback_id);
} else if modifiers.ctrl
&& modifiers.shift
&& modifiers.alt
&& keysym.modified_sym() == keysyms::KEY_Escape
{
return FilterResult::Intercept(CallbackId(999999));
} else if modifiers.ctrl && modifiers.alt {
if let mut vt @ keysyms::KEY_XF86Switch_VT_1..=keysyms::KEY_XF86Switch_VT_12 =
keysym.modified_sym() {
vt = vt - keysyms::KEY_XF86Switch_VT_1 + 1;
return FilterResult::Intercept(CallbackId(1000000 + vt));
}
}
}
if keysym.modified_sym() == keysyms::KEY_Control_L {
match press_state {
KeyState::Pressed => {
move_mode = true;
}
KeyState::Released => {
move_mode = false;
}
}
}
FilterResult::Forward
},
);
self.move_mode = move_mode;
if let Some(callback_id) = action {
if callback_id.0 == 999999 {
self.loop_signal.stop();
return;
}
if callback_id.0 > 1000000 {
let vt = callback_id.0 - 1000000;
tracing::info!("Switching to vt {vt}");
if let Err(err) = self.backend_data.session.change_vt(vt as i32) {
tracing::error!("Failed to switch to vt {vt}: {err}");
}
return;
}
if let Some(stream) = self.api_state.stream.as_ref() {
if let Err(err) = crate::api::send_to_client(
&mut stream.lock().expect("Could not lock stream mutex"),
&OutgoingMsg::CallCallback {
callback_id,
args: None,
},
) {
tracing::warn!("error sending msg to client: {err}");
}
}
}
}
fn pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) { fn pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) {
let serial = SERIAL_COUNTER.next_serial(); let serial = SERIAL_COUNTER.next_serial();
self.pointer_location += event.delta(); self.pointer_location += event.delta();