input: add basic touch support
Browse files at this commit719697179f6ddd79206e6ccf058a5e43f18ba346Parent: 5ac350d4 changed files +168 -6
- niri-config/src/lib.rs +15 -0
- resources/default-config.kdl +7 -0
- src/input.rs +132 -6
- src/niri.rs +14 -0
niri-config/src/lib.rsmodified+15 -0
| @@ -62,6 +62,8 @@ pub struct Input { | |||
| 62 | pub trackpoint: Trackpoint, | 62 | pub trackpoint: Trackpoint, |
| 63 | #[knuffel(child, default)] | 63 | #[knuffel(child, default)] |
| 64 | pub tablet: Tablet, | 64 | pub tablet: Tablet, |
| 65 | + #[knuffel(child, default)] | ||
| 66 | + pub touch: Touch, | ||
| 65 | #[knuffel(child)] | 67 | #[knuffel(child)] |
| 66 | pub disable_power_key_handling: bool, | 68 | pub disable_power_key_handling: bool, |
| 67 | } | 69 | } |
| @@ -201,6 +203,12 @@ pub struct Tablet { | |||
| 201 | pub map_to_output: Option<String>, | 203 | pub map_to_output: Option<String>, |
| 202 | } | 204 | } |
| 203 | 205 | ||
| 206 | +#[derive(knuffel::Decode, Debug, Default, PartialEq)] | ||
| 207 | +pub struct Touch { | ||
| 208 | + #[knuffel(child, unwrap(argument))] | ||
| 209 | + pub map_to_output: Option<String>, | ||
| 210 | +} | ||
| 211 | + | ||
| 204 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] | 212 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] |
| 205 | pub struct Output { | 213 | pub struct Output { |
| 206 | #[knuffel(child)] | 214 | #[knuffel(child)] |
| @@ -1092,6 +1100,10 @@ mod tests { | |||
| 1092 | map-to-output "eDP-1" | 1100 | map-to-output "eDP-1" |
| 1093 | } | 1101 | } |
| 1094 | 1102 | ||
| 1103 | + touch { | ||
| 1104 | + map-to-output "eDP-1" | ||
| 1105 | + } | ||
| 1106 | + | ||
| 1095 | disable-power-key-handling | 1107 | disable-power-key-handling |
| 1096 | } | 1108 | } |
| 1097 | 1109 | ||
| @@ -1223,6 +1235,9 @@ mod tests { | |||
| 1223 | tablet: Tablet { | 1235 | tablet: Tablet { |
| 1224 | map_to_output: Some("eDP-1".to_owned()), | 1236 | map_to_output: Some("eDP-1".to_owned()), |
| 1225 | }, | 1237 | }, |
| 1238 | + touch: Touch { | ||
| 1239 | + map_to_output: Some("eDP-1".to_owned()), | ||
| 1240 | + }, | ||
| 1226 | disable_power_key_handling: true, | 1241 | disable_power_key_handling: true, |
| 1227 | }, | 1242 | }, |
| 1228 | outputs: vec![Output { | 1243 | outputs: vec![Output { |
resources/default-config.kdlmodified+7 -0
| @@ -54,6 +54,13 @@ input { | |||
| 54 | map-to-output "eDP-1" | 54 | map-to-output "eDP-1" |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | + touch { | ||
| 58 | + // Set the name of the output (see below) which touch input will map to. | ||
| 59 | + // If this is unset or the output doesn't exist, touch input maps to one of the | ||
| 60 | + // existing outputs. | ||
| 61 | + map-to-output "eDP-1" | ||
| 62 | + } | ||
| 63 | + | ||
| 57 | // By default, niri will take over the power button to make it sleep | 64 | // By default, niri will take over the power button to make it sleep |
| 58 | // instead of power off. | 65 | // instead of power off. |
| 59 | // Uncomment this if you would like to configure the power button elsewhere | 66 | // Uncomment this if you would like to configure the power button elsewhere |
src/input.rsmodified+132 -6
| @@ -8,7 +8,7 @@ use smithay::backend::input::{ | |||
| 8 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, | 8 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, |
| 9 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, | 9 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, |
| 10 | PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent, | 10 | PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent, |
| 11 | - TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, | 11 | + TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, TouchEvent, |
| 12 | }; | 12 | }; |
| 13 | use smithay::backend::libinput::LibinputInputBackend; | 13 | use smithay::backend::libinput::LibinputInputBackend; |
| 14 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; | 14 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; |
| @@ -17,6 +17,7 @@ use smithay::input::pointer::{ | |||
| 17 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, | 17 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, |
| 18 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent, | 18 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent, |
| 19 | }; | 19 | }; |
| 20 | +use smithay::input::touch::{DownEvent, MotionEvent as TouchMotionEvent, UpEvent}; | ||
| 20 | use smithay::utils::{Logical, Point, SERIAL_COUNTER}; | 21 | use smithay::utils::{Logical, Point, SERIAL_COUNTER}; |
| 21 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; | 22 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; |
| 22 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | 23 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| @@ -101,11 +102,11 @@ impl State { | |||
| 101 | GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event), | 102 | GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event), |
| 102 | GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event), | 103 | GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event), |
| 103 | GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event), | 104 | GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event), |
| 104 | - TouchDown { .. } => (), | 105 | + TouchDown { event } => self.on_touch_down::<I>(event), |
| 105 | - TouchMotion { .. } => (), | 106 | + TouchMotion { event } => self.on_touch_motion::<I>(event), |
| 106 | - TouchUp { .. } => (), | 107 | + TouchUp { event } => self.on_touch_up::<I>(event), |
| 107 | - TouchCancel { .. } => (), | 108 | + TouchCancel { event } => self.on_touch_cancel::<I>(event), |
| 108 | - TouchFrame { .. } => (), | 109 | + TouchFrame { event } => self.on_touch_frame::<I>(event), |
| 109 | SwitchToggle { .. } => (), | 110 | SwitchToggle { .. } => (), |
| 110 | Special(_) => (), | 111 | Special(_) => (), |
| 111 | } | 112 | } |
| @@ -154,9 +155,14 @@ impl State { | |||
| 154 | } | 155 | } |
| 155 | } | 156 | } |
| 156 | 157 | ||
| 158 | + if device.has_capability(input::DeviceCapability::Touch) { | ||
| 159 | + self.niri.touch.insert(device.clone()); | ||
| 160 | + } | ||
| 161 | + | ||
| 157 | apply_libinput_settings(&self.niri.config.borrow().input, device); | 162 | apply_libinput_settings(&self.niri.config.borrow().input, device); |
| 158 | } | 163 | } |
| 159 | InputEvent::DeviceRemoved { device } => { | 164 | InputEvent::DeviceRemoved { device } => { |
| 165 | + self.niri.touch.remove(device); | ||
| 160 | self.niri.tablets.remove(device); | 166 | self.niri.tablets.remove(device); |
| 161 | self.niri.devices.remove(device); | 167 | self.niri.devices.remove(device); |
| 162 | } | 168 | } |
| @@ -171,6 +177,9 @@ impl State { | |||
| 171 | let desc = TabletDescriptor::from(&device); | 177 | let desc = TabletDescriptor::from(&device); |
| 172 | tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc); | 178 | tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc); |
| 173 | } | 179 | } |
| 180 | + if device.has_capability(DeviceCapability::Touch) && self.niri.seat.get_touch().is_none() { | ||
| 181 | + self.niri.seat.add_touch(); | ||
| 182 | + } | ||
| 174 | } | 183 | } |
| 175 | 184 | ||
| 176 | fn on_device_removed(&mut self, device: impl Device) { | 185 | fn on_device_removed(&mut self, device: impl Device) { |
| @@ -185,6 +194,9 @@ impl State { | |||
| 185 | tablet_seat.clear_tools(); | 194 | tablet_seat.clear_tools(); |
| 186 | } | 195 | } |
| 187 | } | 196 | } |
| 197 | + if device.has_capability(DeviceCapability::Touch) && self.niri.touch.is_empty() { | ||
| 198 | + self.niri.seat.remove_touch(); | ||
| 199 | + } | ||
| 188 | } | 200 | } |
| 189 | 201 | ||
| 190 | /// Computes the cursor position for the tablet event. | 202 | /// Computes the cursor position for the tablet event. |
| @@ -283,6 +295,10 @@ impl State { | |||
| 283 | return; | 295 | return; |
| 284 | } | 296 | } |
| 285 | 297 | ||
| 298 | + if let Some(touch) = self.niri.seat.get_touch() { | ||
| 299 | + touch.cancel(self); | ||
| 300 | + } | ||
| 301 | + | ||
| 286 | match action { | 302 | match action { |
| 287 | Action::Quit(skip_confirmation) => { | 303 | Action::Quit(skip_confirmation) => { |
| 288 | if !skip_confirmation { | 304 | if !skip_confirmation { |
| @@ -1354,6 +1370,116 @@ impl State { | |||
| 1354 | }, | 1370 | }, |
| 1355 | ); | 1371 | ); |
| 1356 | } | 1372 | } |
| 1373 | + | ||
| 1374 | + /// Computes the cursor position for the touch event. | ||
| 1375 | + /// | ||
| 1376 | + /// This function handles the touch output mapping, as well as coordinate transform | ||
| 1377 | + fn compute_touch_location<I: InputBackend, E: AbsolutePositionEvent<I>>( | ||
| 1378 | + &self, | ||
| 1379 | + evt: &E, | ||
| 1380 | + ) -> Option<Point<f64, Logical>> { | ||
| 1381 | + let output = self.niri.output_for_touch()?; | ||
| 1382 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 1383 | + let transform = output.current_transform(); | ||
| 1384 | + let size = transform.invert().transform_size(output_geo.size); | ||
| 1385 | + Some( | ||
| 1386 | + transform.transform_point_in(evt.position_transformed(size), &size.to_f64()) | ||
| 1387 | + + output_geo.loc.to_f64(), | ||
| 1388 | + ) | ||
| 1389 | + } | ||
| 1390 | + | ||
| 1391 | + fn on_touch_down<I: InputBackend>(&mut self, evt: I::TouchDownEvent) { | ||
| 1392 | + let Some(handle) = self.niri.seat.get_touch() else { | ||
| 1393 | + return; | ||
| 1394 | + }; | ||
| 1395 | + let Some(touch_location) = self.compute_touch_location(&evt) else { | ||
| 1396 | + return; | ||
| 1397 | + }; | ||
| 1398 | + | ||
| 1399 | + if !handle.is_grabbed() { | ||
| 1400 | + let output_under_touch = self | ||
| 1401 | + .niri | ||
| 1402 | + .global_space | ||
| 1403 | + .output_under(touch_location) | ||
| 1404 | + .next() | ||
| 1405 | + .cloned(); | ||
| 1406 | + if let Some(window) = self.niri.window_under(touch_location) { | ||
| 1407 | + let window = window.clone(); | ||
| 1408 | + self.niri.layout.activate_window(&window); | ||
| 1409 | + | ||
| 1410 | + // FIXME: granular. | ||
| 1411 | + self.niri.queue_redraw_all(); | ||
| 1412 | + } else if let Some(output) = output_under_touch { | ||
| 1413 | + self.niri.layout.activate_output(&output); | ||
| 1414 | + | ||
| 1415 | + // FIXME: granular. | ||
| 1416 | + self.niri.queue_redraw_all(); | ||
| 1417 | + }; | ||
| 1418 | + }; | ||
| 1419 | + | ||
| 1420 | + let serial = SERIAL_COUNTER.next_serial(); | ||
| 1421 | + let under = self | ||
| 1422 | + .niri | ||
| 1423 | + .surface_under_and_global_space(touch_location) | ||
| 1424 | + .map(|under| under.surface); | ||
| 1425 | + handle.down( | ||
| 1426 | + self, | ||
| 1427 | + under, | ||
| 1428 | + &DownEvent { | ||
| 1429 | + slot: evt.slot(), | ||
| 1430 | + location: touch_location, | ||
| 1431 | + serial, | ||
| 1432 | + time: evt.time_msec(), | ||
| 1433 | + }, | ||
| 1434 | + ); | ||
| 1435 | + } | ||
| 1436 | + fn on_touch_up<I: InputBackend>(&mut self, evt: I::TouchUpEvent) { | ||
| 1437 | + let Some(handle) = self.niri.seat.get_touch() else { | ||
| 1438 | + return; | ||
| 1439 | + }; | ||
| 1440 | + let serial = SERIAL_COUNTER.next_serial(); | ||
| 1441 | + handle.up( | ||
| 1442 | + self, | ||
| 1443 | + &UpEvent { | ||
| 1444 | + slot: evt.slot(), | ||
| 1445 | + serial, | ||
| 1446 | + time: evt.time_msec(), | ||
| 1447 | + }, | ||
| 1448 | + ) | ||
| 1449 | + } | ||
| 1450 | + fn on_touch_motion<I: InputBackend>(&mut self, evt: I::TouchMotionEvent) { | ||
| 1451 | + let Some(handle) = self.niri.seat.get_touch() else { | ||
| 1452 | + return; | ||
| 1453 | + }; | ||
| 1454 | + let Some(touch_location) = self.compute_touch_location(&evt) else { | ||
| 1455 | + return; | ||
| 1456 | + }; | ||
| 1457 | + let under = self | ||
| 1458 | + .niri | ||
| 1459 | + .surface_under_and_global_space(touch_location) | ||
| 1460 | + .map(|under| under.surface); | ||
| 1461 | + handle.motion( | ||
| 1462 | + self, | ||
| 1463 | + under, | ||
| 1464 | + &TouchMotionEvent { | ||
| 1465 | + slot: evt.slot(), | ||
| 1466 | + location: touch_location, | ||
| 1467 | + time: evt.time_msec(), | ||
| 1468 | + }, | ||
| 1469 | + ); | ||
| 1470 | + } | ||
| 1471 | + fn on_touch_frame<I: InputBackend>(&mut self, _evt: I::TouchFrameEvent) { | ||
| 1472 | + let Some(handle) = self.niri.seat.get_touch() else { | ||
| 1473 | + return; | ||
| 1474 | + }; | ||
| 1475 | + handle.frame(self); | ||
| 1476 | + } | ||
| 1477 | + fn on_touch_cancel<I: InputBackend>(&mut self, _evt: I::TouchCancelEvent) { | ||
| 1478 | + let Some(handle) = self.niri.seat.get_touch() else { | ||
| 1479 | + return; | ||
| 1480 | + }; | ||
| 1481 | + handle.cancel(self); | ||
| 1482 | + } | ||
| 1357 | } | 1483 | } |
| 1358 | 1484 | ||
| 1359 | /// Check whether the key should be intercepted and mark intercepted | 1485 | /// Check whether the key should be intercepted and mark intercepted |
src/niri.rsmodified+14 -0
| @@ -144,6 +144,7 @@ pub struct Niri { | |||
| 144 | 144 | ||
| 145 | pub devices: HashSet<input::Device>, | 145 | pub devices: HashSet<input::Device>, |
| 146 | pub tablets: HashMap<input::Device, TabletData>, | 146 | pub tablets: HashMap<input::Device, TabletData>, |
| 147 | + pub touch: HashSet<input::Device>, | ||
| 147 | 148 | ||
| 148 | // Smithay state. | 149 | // Smithay state. |
| 149 | pub compositor_state: CompositorState, | 150 | pub compositor_state: CompositorState, |
| @@ -702,6 +703,10 @@ impl State { | |||
| 702 | self.niri.reposition_outputs(None); | 703 | self.niri.reposition_outputs(None); |
| 703 | 704 | ||
| 704 | self.backend.on_output_config_changed(&mut self.niri); | 705 | self.backend.on_output_config_changed(&mut self.niri); |
| 706 | + | ||
| 707 | + if let Some(touch) = self.niri.seat.get_touch() { | ||
| 708 | + touch.cancel(self); | ||
| 709 | + } | ||
| 705 | } | 710 | } |
| 706 | 711 | ||
| 707 | // Can't really update xdg-decoration settings since we have to hide the globals for CSD | 712 | // Can't really update xdg-decoration settings since we have to hide the globals for CSD |
| @@ -1002,6 +1007,7 @@ impl Niri { | |||
| 1002 | 1007 | ||
| 1003 | devices: HashSet::new(), | 1008 | devices: HashSet::new(), |
| 1004 | tablets: HashMap::new(), | 1009 | tablets: HashMap::new(), |
| 1010 | + touch: HashSet::new(), | ||
| 1005 | 1011 | ||
| 1006 | compositor_state, | 1012 | compositor_state, |
| 1007 | xdg_shell_state, | 1013 | xdg_shell_state, |
| @@ -1602,6 +1608,14 @@ impl Niri { | |||
| 1602 | .or_else(|| self.global_space.outputs().next()) | 1608 | .or_else(|| self.global_space.outputs().next()) |
| 1603 | } | 1609 | } |
| 1604 | 1610 | ||
| 1611 | + pub fn output_for_touch(&self) -> Option<&Output> { | ||
| 1612 | + let config = self.config.borrow(); | ||
| 1613 | + let map_to_output = config.input.touch.map_to_output.as_ref(); | ||
| 1614 | + map_to_output | ||
| 1615 | + .and_then(|name| self.output_by_name.get(name)) | ||
| 1616 | + .or_else(|| self.global_space.outputs().next()) | ||
| 1617 | + } | ||
| 1618 | + | ||
| 1605 | pub fn output_for_root(&self, root: &WlSurface) -> Option<&Output> { | 1619 | pub fn output_for_root(&self, root: &WlSurface) -> Option<&Output> { |
| 1606 | // Check the main layout. | 1620 | // Check the main layout. |
| 1607 | let win_out = self.layout.find_window_and_output(root); | 1621 | let win_out = self.layout.find_window_and_output(root); |