From 7123ecf8bad982eddec9304270dcd014da19e8aa Mon Sep 17 00:00:00 2001 From: Ottatop Date: Mon, 7 Aug 2023 19:48:18 -0500 Subject: [PATCH] Add vt switching --- src/backend/udev.rs | 2 +- src/input.rs | 148 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 129 insertions(+), 21 deletions(-) diff --git a/src/backend/udev.rs b/src/backend/udev.rs index 21a01ae..6166e0e 100644 --- a/src/backend/udev.rs +++ b/src/backend/udev.rs @@ -122,7 +122,7 @@ struct UdevOutputId { } pub struct UdevData { - session: LibSeatSession, + pub session: LibSeatSession, display_handle: DisplayHandle, dmabuf_state: Option<(DmabufState, DmabufGlobal)>, primary_gpu: DrmNode, diff --git a/src/input.rs b/src/input.rs index 70d21a6..9c33dd5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -8,10 +8,10 @@ use crate::{ window::WindowElement, }; use smithay::{ - backend::input::{ + backend::{input::{ AbsolutePositionEvent, Axis, AxisSource, ButtonState, Event, InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, PointerMotionEvent, - }, + }, session::Session}, desktop::{layer_map_for_output, space::SpaceElement}, input::{ keyboard::{keysyms, FilterResult}, @@ -304,6 +304,26 @@ impl State { .axis(self, frame); } + +} + +impl State { + pub fn process_input_event(&mut self, event: InputEvent) { + match event { + // TODO: rest of input events + + // InputEvent::DeviceAdded { device } => todo!(), + // InputEvent::DeviceRemoved { device } => todo!(), + InputEvent::Keyboard { event } => self.keyboard::(event), + // InputEvent::PointerMotion { event } => {} + InputEvent::PointerMotionAbsolute { event } => self.pointer_motion_absolute::(event), + InputEvent::PointerButton { event } => self.pointer_button::(event), + InputEvent::PointerAxis { event } => self.pointer_axis::(event), + + _ => (), + } + } + fn keyboard(&mut self, event: I::KeyboardKeyEvent) { let serial = SERIAL_COUNTER.next_serial(); let time = event.time_msec(); @@ -351,6 +371,13 @@ impl State { && 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}"); + + } } } @@ -387,24 +414,6 @@ impl State { } } } -} - -impl State { - pub fn process_input_event(&mut self, event: InputEvent) { - match event { - // TODO: rest of input events - - // InputEvent::DeviceAdded { device } => todo!(), - // InputEvent::DeviceRemoved { device } => todo!(), - InputEvent::Keyboard { event } => self.keyboard::(event), - // InputEvent::PointerMotion { event } => {} - InputEvent::PointerMotionAbsolute { event } => self.pointer_motion_absolute::(event), - InputEvent::PointerButton { event } => self.pointer_button::(event), - InputEvent::PointerAxis { event } => self.pointer_axis::(event), - - _ => (), - } - } fn pointer_motion_absolute(&mut self, event: I::PointerMotionAbsoluteEvent) { let Some(output) = self.space.outputs().next() else { return; }; @@ -470,6 +479,105 @@ impl State { } } + fn keyboard(&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::::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(&mut self, event: I::PointerMotionEvent) { let serial = SERIAL_COUNTER.next_serial(); self.pointer_location += event.delta();