|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +pub use smithay::{ |
| 3 | + backend::input::KeyState, |
| 4 | + input::keyboard::{keysyms as KeySyms, Keysym, ModifiersState}, |
| 5 | + output::{Mode, Output}, |
| 6 | + reexports::{ |
| 7 | + calloop::LoopHandle, |
| 8 | + input::{ |
| 9 | + AccelProfile, ClickMethod, Device as InputDevice, ScrollMethod, SendEventsMode, |
| 10 | + TapButtonMap, |
| 11 | + }, |
| 12 | + }, |
| 13 | + utils::{Logical, Physical, Point, Size, Transform}, |
| 14 | +}; |
| 15 | +use tracing::warn; |
| 16 | + |
| 17 | +use super::types::*; |
| 18 | + |
| 19 | +#[derive(Debug, Deserialize, Serialize)] |
| 20 | +pub struct InputConfig { |
| 21 | + state: DeviceState, |
| 22 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 23 | + acceleration: Option<AccelConfig>, |
| 24 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 25 | + calibration: Option<[f32; 6]>, |
| 26 | + #[serde(with = "ClickMethodDef")] |
| 27 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 28 | + click_method: Option<ClickMethod>, |
| 29 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 30 | + disable_while_typing: Option<bool>, |
| 31 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 32 | + left_handed: Option<bool>, |
| 33 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 34 | + middle_button_emulation: Option<bool>, |
| 35 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 36 | + rotation_angle: Option<u32>, |
| 37 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 38 | + scroll_config: Option<ScrollConfig>, |
| 39 | + #[serde(skip_serializing_if = "Option::is_none", default)] |
| 40 | + tap_config: Option<TapConfig>, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Deserialize, Serialize)] |
| 44 | +pub struct AccelConfig { |
| 45 | + #[serde(with = "AccelProfileDef")] |
| 46 | + profile: Option<AccelProfile>, |
| 47 | + speed: f64, |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, Deserialize, Serialize)] |
| 51 | +pub struct ScrollConfig { |
| 52 | + #[serde(with = "ScrollMethodDef")] |
| 53 | + method: Option<ScrollMethod>, |
| 54 | + natural_scroll: Option<bool>, |
| 55 | + scroll_button: Option<u32>, |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Debug, Serialize, Deserialize)] |
| 59 | +pub enum DeviceState { |
| 60 | + Enabled, |
| 61 | + Disabled, |
| 62 | + DisabledOnExternalMouse, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Debug, Serialize, Deserialize)] |
| 66 | +pub struct TapConfig { |
| 67 | + enabled: bool, |
| 68 | + #[serde(with = "TapButtonMapDef")] |
| 69 | + button_map: Option<TapButtonMap>, |
| 70 | + drag: bool, |
| 71 | + drag_lock: bool, |
| 72 | +} |
| 73 | + |
| 74 | +impl InputConfig { |
| 75 | + pub fn for_device(device: &InputDevice) -> Self { |
| 76 | + InputConfig { |
| 77 | + state: match device.config_send_events_mode() { |
| 78 | + x if x.contains(SendEventsMode::ENABLED) => DeviceState::Enabled, |
| 79 | + x if x.contains(SendEventsMode::DISABLED_ON_EXTERNAL_MOUSE) => { |
| 80 | + DeviceState::DisabledOnExternalMouse |
| 81 | + } |
| 82 | + x if x.contains(SendEventsMode::DISABLED) => DeviceState::Disabled, |
| 83 | + _ => DeviceState::Disabled, |
| 84 | + }, |
| 85 | + acceleration: if device.config_accel_is_available() { |
| 86 | + Some(AccelConfig { |
| 87 | + profile: device.config_accel_profile(), |
| 88 | + speed: device.config_accel_speed(), |
| 89 | + }) |
| 90 | + } else { |
| 91 | + None |
| 92 | + }, |
| 93 | + calibration: device.config_calibration_matrix(), |
| 94 | + click_method: device.config_click_method(), |
| 95 | + disable_while_typing: if device.config_dwt_is_available() { |
| 96 | + Some(device.config_dwt_enabled()) |
| 97 | + } else { |
| 98 | + None |
| 99 | + }, |
| 100 | + left_handed: if device.config_left_handed_is_available() { |
| 101 | + Some(device.config_left_handed()) |
| 102 | + } else { |
| 103 | + None |
| 104 | + }, |
| 105 | + middle_button_emulation: if device.config_middle_emulation_is_available() { |
| 106 | + Some(device.config_middle_emulation_enabled()) |
| 107 | + } else { |
| 108 | + None |
| 109 | + }, |
| 110 | + rotation_angle: if device.config_rotation_is_available() { |
| 111 | + Some(device.config_rotation_angle()) |
| 112 | + } else { |
| 113 | + None |
| 114 | + }, |
| 115 | + scroll_config: if device |
| 116 | + .config_scroll_methods() |
| 117 | + .iter() |
| 118 | + .any(|x| *x != ScrollMethod::NoScroll) |
| 119 | + { |
| 120 | + Some(ScrollConfig { |
| 121 | + method: device.config_scroll_method(), |
| 122 | + natural_scroll: if device.config_scroll_has_natural_scroll() { |
| 123 | + Some(device.config_scroll_natural_scroll_enabled()) |
| 124 | + } else { |
| 125 | + None |
| 126 | + }, |
| 127 | + scroll_button: if device.config_scroll_method() |
| 128 | + == Some(ScrollMethod::OnButtonDown) |
| 129 | + { |
| 130 | + Some(device.config_scroll_button()) |
| 131 | + } else { |
| 132 | + None |
| 133 | + }, |
| 134 | + }) |
| 135 | + } else { |
| 136 | + None |
| 137 | + }, |
| 138 | + tap_config: if device.config_tap_finger_count() > 0 { |
| 139 | + Some(TapConfig { |
| 140 | + enabled: device.config_tap_enabled(), |
| 141 | + button_map: device.config_tap_button_map(), |
| 142 | + drag: device.config_tap_drag_enabled(), |
| 143 | + drag_lock: device.config_tap_drag_lock_enabled(), |
| 144 | + }) |
| 145 | + } else { |
| 146 | + None |
| 147 | + }, |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + pub fn update_device(&self, device: &mut InputDevice) { |
| 152 | + if let Err(err) = match self.state { |
| 153 | + DeviceState::Enabled => device.config_send_events_set_mode(SendEventsMode::ENABLED), |
| 154 | + DeviceState::Disabled => device.config_send_events_set_mode(SendEventsMode::DISABLED), |
| 155 | + DeviceState::DisabledOnExternalMouse => { |
| 156 | + device.config_send_events_set_mode(SendEventsMode::DISABLED_ON_EXTERNAL_MOUSE) |
| 157 | + } |
| 158 | + } { |
| 159 | + warn!( |
| 160 | + ?err, |
| 161 | + "Failed to apply mode {:?} for device {:?}.", |
| 162 | + self.state, |
| 163 | + device.name(), |
| 164 | + ); |
| 165 | + } |
| 166 | + if let Some(accel) = self.acceleration.as_ref() { |
| 167 | + if let Some(profile) = accel.profile { |
| 168 | + if let Err(err) = device.config_accel_set_profile(profile) { |
| 169 | + warn!( |
| 170 | + ?err, |
| 171 | + "Failed to apply acceleration profile {:?} for device {:?}.", |
| 172 | + profile, |
| 173 | + device.name(), |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + if let Err(err) = device.config_accel_set_speed(accel.speed) { |
| 178 | + warn!( |
| 179 | + ?err, |
| 180 | + "Failed to apply acceleration speed {:?} for device {:?}.", |
| 181 | + accel.speed, |
| 182 | + device.name(), |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | + if let Some(matrix) = self.calibration { |
| 187 | + if let Err(err) = device.config_calibration_set_matrix(matrix) { |
| 188 | + warn!( |
| 189 | + ?err, |
| 190 | + "Failed to apply calibration matrix {:?} for device {:?}.", |
| 191 | + matrix, |
| 192 | + device.name(), |
| 193 | + ); |
| 194 | + } |
| 195 | + } |
| 196 | + if let Some(method) = self.click_method { |
| 197 | + if let Err(err) = device.config_click_set_method(method) { |
| 198 | + warn!( |
| 199 | + ?err, |
| 200 | + "Failed to apply click method {:?} for device {:?}.", |
| 201 | + method, |
| 202 | + device.name(), |
| 203 | + ); |
| 204 | + } |
| 205 | + } |
| 206 | + if let Some(dwt) = self.disable_while_typing { |
| 207 | + if let Err(err) = device.config_dwt_set_enabled(dwt) { |
| 208 | + warn!( |
| 209 | + ?err, |
| 210 | + "Failed to apply disable-while-typing {:?} for device {:?}.", |
| 211 | + dwt, |
| 212 | + device.name(), |
| 213 | + ); |
| 214 | + } |
| 215 | + } |
| 216 | + if let Some(left) = self.left_handed { |
| 217 | + if let Err(err) = device.config_left_handed_set(left) { |
| 218 | + warn!( |
| 219 | + ?err, |
| 220 | + "Failed to apply left-handed {:?} for device {:?}.", |
| 221 | + left, |
| 222 | + device.name(), |
| 223 | + ); |
| 224 | + } |
| 225 | + } |
| 226 | + if let Some(middle) = self.middle_button_emulation { |
| 227 | + if let Err(err) = device.config_middle_emulation_set_enabled(middle) { |
| 228 | + warn!( |
| 229 | + ?err, |
| 230 | + "Failed to apply middle-button-emulation {:?} for device {:?}.", |
| 231 | + middle, |
| 232 | + device.name(), |
| 233 | + ); |
| 234 | + } |
| 235 | + } |
| 236 | + if let Some(angle) = self.rotation_angle { |
| 237 | + if let Err(err) = device.config_rotation_set_angle(angle) { |
| 238 | + warn!( |
| 239 | + ?err, |
| 240 | + "Failed to apply rotation-angle {:?} for device {:?}", |
| 241 | + angle, |
| 242 | + device.name(), |
| 243 | + ); |
| 244 | + } |
| 245 | + } |
| 246 | + if let Some(scroll) = self.scroll_config.as_ref() { |
| 247 | + if let Some(method) = scroll.method { |
| 248 | + if let Err(err) = device.config_scroll_set_method(method) { |
| 249 | + warn!( |
| 250 | + ?err, |
| 251 | + "Failed to apply scroll method {:?} for device {:?}.", |
| 252 | + method, |
| 253 | + device.name(), |
| 254 | + ); |
| 255 | + } |
| 256 | + } |
| 257 | + if let Some(natural) = scroll.natural_scroll { |
| 258 | + if let Err(err) = device.config_scroll_set_natural_scroll_enabled(natural) { |
| 259 | + warn!( |
| 260 | + ?err, |
| 261 | + "Failed to apply natural scrolling {:?} for device {:?}.", |
| 262 | + natural, |
| 263 | + device.name(), |
| 264 | + ); |
| 265 | + } |
| 266 | + } |
| 267 | + if let Some(button) = scroll.scroll_button { |
| 268 | + if let Err(err) = device.config_scroll_set_button(button) { |
| 269 | + warn!( |
| 270 | + ?err, |
| 271 | + "Failed to apply scroll button {:?} for device {:?}.", |
| 272 | + button, |
| 273 | + device.name(), |
| 274 | + ); |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + if let Some(tap) = self.tap_config.as_ref() { |
| 279 | + if let Err(err) = device.config_tap_set_enabled(tap.enabled) { |
| 280 | + warn!( |
| 281 | + ?err, |
| 282 | + "Failed to apply tap-to-click {:?} for device {:?}.", |
| 283 | + tap.enabled, |
| 284 | + device.name(), |
| 285 | + ); |
| 286 | + } |
| 287 | + if let Some(button_map) = tap.button_map { |
| 288 | + if let Err(err) = device.config_tap_set_button_map(button_map) { |
| 289 | + warn!( |
| 290 | + ?err, |
| 291 | + "Failed to apply button map {:?} for device {:?}.", |
| 292 | + button_map, |
| 293 | + device.name(), |
| 294 | + ); |
| 295 | + } |
| 296 | + } |
| 297 | + if let Err(err) = device.config_tap_set_drag_enabled(tap.drag) { |
| 298 | + warn!( |
| 299 | + ?err, |
| 300 | + "Failed to apply tap-drag {:?} for device {:?}.", |
| 301 | + tap.drag, |
| 302 | + device.name(), |
| 303 | + ); |
| 304 | + } |
| 305 | + if let Err(err) = device.config_tap_set_drag_lock_enabled(tap.drag_lock) { |
| 306 | + warn!( |
| 307 | + ?err, |
| 308 | + "Failed to apply tap-drag-lock {:?} for device {:?}.", |
| 309 | + tap.drag_lock, |
| 310 | + device.name(), |
| 311 | + ); |
| 312 | + } |
| 313 | + } |
| 314 | + } |
| 315 | +} |
0 commit comments