Extract input handlers to functions
Browse files at this commit23a5bd36701f1a8da8e886d28d64e4fa8b46042bParent: d3973751 changed file +887 -843
- src/input.rs +887 -843
src/input.rsmodified+887 -843
| @@ -53,952 +53,996 @@ impl State { | |||
| 53 | self.niri.activate_monitors(&self.backend); | 53 | self.niri.activate_monitors(&self.backend); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | - let comp_mod = self.backend.mod_key(); | 56 | + use InputEvent::*; |
| 57 | - | ||
| 58 | match event { | 57 | match event { |
| 59 | - InputEvent::Keyboard { event, .. } => { | 58 | + DeviceAdded { device } => self.on_device_added(device), |
| 60 | - let serial = SERIAL_COUNTER.next_serial(); | 59 | + DeviceRemoved { device } => self.on_device_removed(device), |
| 61 | - let time = Event::time_msec(&event); | 60 | + Keyboard { event } => self.on_keyboard::<I>(event), |
| 62 | - let pressed = event.state() == KeyState::Pressed; | 61 | + PointerMotion { event } => self.on_pointer_motion::<I>(event), |
| 62 | + PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event), | ||
| 63 | + PointerButton { event } => self.on_pointer_button::<I>(event), | ||
| 64 | + PointerAxis { event } => self.on_pointer_axis::<I>(event), | ||
| 65 | + TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event), | ||
| 66 | + TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event), | ||
| 67 | + TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event), | ||
| 68 | + TabletToolButton { event } => self.on_tablet_tool_button::<I>(event), | ||
| 69 | + GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event), | ||
| 70 | + GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event), | ||
| 71 | + GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event), | ||
| 72 | + GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event), | ||
| 73 | + GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event), | ||
| 74 | + GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event), | ||
| 75 | + GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event), | ||
| 76 | + GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event), | ||
| 77 | + TouchDown { .. } => (), | ||
| 78 | + TouchMotion { .. } => (), | ||
| 79 | + TouchUp { .. } => (), | ||
| 80 | + TouchCancel { .. } => (), | ||
| 81 | + TouchFrame { .. } => (), | ||
| 82 | + Special(_) => (), | ||
| 83 | + } | ||
| 84 | + } | ||
| 63 | 85 | ||
| 64 | - let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input( | 86 | + pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| 65 | - self, | 87 | + match event { |
| 66 | - event.key_code(), | 88 | + InputEvent::DeviceAdded { device } => { |
| 67 | - event.state(), | 89 | + // According to Mutter code, this setting is specific to touchpads. |
| 68 | - serial, | 90 | + let is_touchpad = device.config_tap_finger_count() > 0; |
| 69 | - time, | 91 | + if is_touchpad { |
| 70 | - |this, mods, keysym| { | 92 | + let c = &self.niri.config.borrow().input.touchpad; |
| 71 | - let bindings = &this.niri.config.borrow().binds; | 93 | + let _ = device.config_tap_set_enabled(c.tap); |
| 72 | - let key_code = event.key_code(); | 94 | + let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 73 | - let modified = keysym.modified_sym(); | 95 | + let _ = device.config_accel_set_speed(c.accel_speed); |
| 74 | - let raw = keysym.raw_latin_sym_or_raw_current_sym(); | 96 | + } |
| 75 | - should_intercept_key( | ||
| 76 | - &mut this.niri.suppressed_keys, | ||
| 77 | - bindings, | ||
| 78 | - comp_mod, | ||
| 79 | - key_code, | ||
| 80 | - modified, | ||
| 81 | - raw, | ||
| 82 | - pressed, | ||
| 83 | - *mods, | ||
| 84 | - &this.niri.screenshot_ui, | ||
| 85 | - ) | ||
| 86 | - }, | ||
| 87 | - ) else { | ||
| 88 | - return; | ||
| 89 | - }; | ||
| 90 | 97 | ||
| 91 | - // Filter actions when the key is released or the session is locked. | 98 | + if device.has_capability(smithay::reexports::input::DeviceCapability::TabletTool) { |
| 92 | - if !pressed { | 99 | + match device.size() { |
| 93 | - return; | 100 | + Some((w, h)) => { |
| 101 | + self.niri.tablets.insert( | ||
| 102 | + device.clone(), | ||
| 103 | + TabletData { | ||
| 104 | + aspect_ratio: w / h, | ||
| 105 | + }, | ||
| 106 | + ); | ||
| 107 | + } | ||
| 108 | + None => { | ||
| 109 | + warn!("tablet tool device has no size"); | ||
| 110 | + } | ||
| 111 | + } | ||
| 94 | } | 112 | } |
| 113 | + } | ||
| 114 | + InputEvent::DeviceRemoved { device } => { | ||
| 115 | + self.niri.tablets.remove(device); | ||
| 116 | + } | ||
| 117 | + _ => (), | ||
| 118 | + } | ||
| 119 | + } | ||
| 95 | 120 | ||
| 96 | - if self.niri.is_locked() | 121 | + fn on_device_added(&mut self, device: impl Device) { |
| 97 | - && !matches!( | 122 | + if device.has_capability(DeviceCapability::TabletTool) { |
| 98 | - action, | 123 | + self.niri |
| 99 | - Action::Quit | 124 | + .seat |
| 100 | - | Action::ChangeVt(_) | 125 | + .tablet_seat() |
| 101 | - | Action::Suspend | 126 | + .add_tablet::<Self>(&self.niri.display_handle, &TabletDescriptor::from(&device)); |
| 102 | - | Action::PowerOffMonitors | 127 | + } |
| 103 | - | Action::SwitchLayout(_) | 128 | + } |
| 104 | - ) | 129 | + |
| 105 | - { | 130 | + fn on_device_removed(&mut self, device: impl Device) { |
| 106 | - return; | 131 | + if device.has_capability(DeviceCapability::TabletTool) { |
| 132 | + let tablet_seat = self.niri.seat.tablet_seat(); | ||
| 133 | + | ||
| 134 | + tablet_seat.remove_tablet(&TabletDescriptor::from(&device)); | ||
| 135 | + | ||
| 136 | + // If there are no tablets in seat we can remove all tools | ||
| 137 | + if tablet_seat.count_tablets() == 0 { | ||
| 138 | + tablet_seat.clear_tools(); | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + fn compute_tablet_position<I: InputBackend>( | ||
| 144 | + &mut self, | ||
| 145 | + event: &(impl Event<I> + TabletToolEvent<I>), | ||
| 146 | + ) -> Option<Point<f64, Logical>> | ||
| 147 | + where | ||
| 148 | + I::Device: 'static, | ||
| 149 | + { | ||
| 150 | + let output = self.niri.output_for_tablet()?; | ||
| 151 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 152 | + | ||
| 153 | + let mut pos = event.position_transformed(output_geo.size); | ||
| 154 | + pos.x /= output_geo.size.w as f64; | ||
| 155 | + pos.y /= output_geo.size.h as f64; | ||
| 156 | + | ||
| 157 | + let device = event.device(); | ||
| 158 | + if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { | ||
| 159 | + if let Some(data) = self.niri.tablets.get(device) { | ||
| 160 | + // This code does the same thing as mutter with "keep aspect ratio" enabled. | ||
| 161 | + let output_aspect_ratio = output_geo.size.w as f64 / output_geo.size.h as f64; | ||
| 162 | + let ratio = data.aspect_ratio / output_aspect_ratio; | ||
| 163 | + | ||
| 164 | + if ratio > 1. { | ||
| 165 | + pos.x *= ratio; | ||
| 166 | + } else { | ||
| 167 | + pos.y /= ratio; | ||
| 107 | } | 168 | } |
| 169 | + } | ||
| 170 | + }; | ||
| 108 | 171 | ||
| 109 | - match action { | 172 | + pos.x *= output_geo.size.w as f64; |
| 110 | - Action::Quit => { | 173 | + pos.y *= output_geo.size.h as f64; |
| 111 | - info!("quitting because quit bind was pressed"); | 174 | + pos.x = pos.x.clamp(0.0, output_geo.size.w as f64 - 1.); |
| 112 | - self.niri.stop_signal.stop() | 175 | + pos.y = pos.y.clamp(0.0, output_geo.size.h as f64 - 1.); |
| 113 | - } | 176 | + Some(pos + output_geo.loc.to_f64()) |
| 114 | - Action::ChangeVt(vt) => { | 177 | + } |
| 115 | - self.backend.change_vt(vt); | ||
| 116 | - // Changing `VT` may not deliver the key releases, so clear the state. | ||
| 117 | - self.niri.suppressed_keys.clear(); | ||
| 118 | - } | ||
| 119 | - Action::Suspend => { | ||
| 120 | - self.backend.suspend(); | ||
| 121 | - // Suspend may not deliver the key releases, so clear the state. | ||
| 122 | - self.niri.suppressed_keys.clear(); | ||
| 123 | - } | ||
| 124 | - Action::PowerOffMonitors => { | ||
| 125 | - self.niri.deactivate_monitors(&self.backend); | ||
| 126 | - } | ||
| 127 | - Action::ToggleDebugTint => { | ||
| 128 | - self.backend.toggle_debug_tint(); | ||
| 129 | - } | ||
| 130 | - Action::Spawn(command) => { | ||
| 131 | - spawn(command); | ||
| 132 | - } | ||
| 133 | - Action::ScreenshotScreen => { | ||
| 134 | - let active = self.niri.layout.active_output().cloned(); | ||
| 135 | - if let Some(active) = active { | ||
| 136 | - if let Some(renderer) = self.backend.renderer() { | ||
| 137 | - if let Err(err) = self.niri.screenshot(renderer, &active) { | ||
| 138 | - warn!("error taking screenshot: {err:?}"); | ||
| 139 | - } | ||
| 140 | - } | ||
| 141 | - } | ||
| 142 | - } | ||
| 143 | - Action::ConfirmScreenshot => { | ||
| 144 | - if let Some(renderer) = self.backend.renderer() { | ||
| 145 | - match self.niri.screenshot_ui.capture(renderer) { | ||
| 146 | - Ok((size, pixels)) => { | ||
| 147 | - if let Err(err) = self.niri.save_screenshot(size, pixels) { | ||
| 148 | - warn!("error saving screenshot: {err:?}"); | ||
| 149 | - } | ||
| 150 | - } | ||
| 151 | - Err(err) => { | ||
| 152 | - warn!("error capturing screenshot: {err:?}"); | ||
| 153 | - } | ||
| 154 | - } | ||
| 155 | - } | ||
| 156 | 178 | ||
| 157 | - self.niri.screenshot_ui.close(); | 179 | + fn on_keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) { |
| 158 | - self.niri | 180 | + let comp_mod = self.backend.mod_key(); |
| 159 | - .cursor_manager | 181 | + |
| 160 | - .set_cursor_image(CursorImageStatus::default_named()); | 182 | + let serial = SERIAL_COUNTER.next_serial(); |
| 161 | - self.niri.queue_redraw_all(); | 183 | + let time = Event::time_msec(&event); |
| 162 | - } | 184 | + let pressed = event.state() == KeyState::Pressed; |
| 163 | - Action::CancelScreenshot => { | 185 | + |
| 164 | - self.niri.screenshot_ui.close(); | 186 | + let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input( |
| 165 | - self.niri | 187 | + self, |
| 166 | - .cursor_manager | 188 | + event.key_code(), |
| 167 | - .set_cursor_image(CursorImageStatus::default_named()); | 189 | + event.state(), |
| 168 | - self.niri.queue_redraw_all(); | 190 | + serial, |
| 169 | - } | 191 | + time, |
| 170 | - Action::Screenshot => { | 192 | + |this, mods, keysym| { |
| 171 | - if let Some(renderer) = self.backend.renderer() { | 193 | + let bindings = &this.niri.config.borrow().binds; |
| 172 | - self.niri.open_screenshot_ui(renderer); | 194 | + let key_code = event.key_code(); |
| 195 | + let modified = keysym.modified_sym(); | ||
| 196 | + let raw = keysym.raw_latin_sym_or_raw_current_sym(); | ||
| 197 | + should_intercept_key( | ||
| 198 | + &mut this.niri.suppressed_keys, | ||
| 199 | + bindings, | ||
| 200 | + comp_mod, | ||
| 201 | + key_code, | ||
| 202 | + modified, | ||
| 203 | + raw, | ||
| 204 | + pressed, | ||
| 205 | + *mods, | ||
| 206 | + &this.niri.screenshot_ui, | ||
| 207 | + ) | ||
| 208 | + }, | ||
| 209 | + ) else { | ||
| 210 | + return; | ||
| 211 | + }; | ||
| 212 | + | ||
| 213 | + // Filter actions when the key is released or the session is locked. | ||
| 214 | + if !pressed { | ||
| 215 | + return; | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + if self.niri.is_locked() | ||
| 219 | + && !matches!( | ||
| 220 | + action, | ||
| 221 | + Action::Quit | ||
| 222 | + | Action::ChangeVt(_) | ||
| 223 | + | Action::Suspend | ||
| 224 | + | Action::PowerOffMonitors | ||
| 225 | + | Action::SwitchLayout(_) | ||
| 226 | + ) | ||
| 227 | + { | ||
| 228 | + return; | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + match action { | ||
| 232 | + Action::Quit => { | ||
| 233 | + info!("quitting because quit bind was pressed"); | ||
| 234 | + self.niri.stop_signal.stop() | ||
| 235 | + } | ||
| 236 | + Action::ChangeVt(vt) => { | ||
| 237 | + self.backend.change_vt(vt); | ||
| 238 | + // Changing `VT` may not deliver the key releases, so clear the state. | ||
| 239 | + self.niri.suppressed_keys.clear(); | ||
| 240 | + } | ||
| 241 | + Action::Suspend => { | ||
| 242 | + self.backend.suspend(); | ||
| 243 | + // Suspend may not deliver the key releases, so clear the state. | ||
| 244 | + self.niri.suppressed_keys.clear(); | ||
| 245 | + } | ||
| 246 | + Action::PowerOffMonitors => { | ||
| 247 | + self.niri.deactivate_monitors(&self.backend); | ||
| 248 | + } | ||
| 249 | + Action::ToggleDebugTint => { | ||
| 250 | + self.backend.toggle_debug_tint(); | ||
| 251 | + } | ||
| 252 | + Action::Spawn(command) => { | ||
| 253 | + spawn(command); | ||
| 254 | + } | ||
| 255 | + Action::ScreenshotScreen => { | ||
| 256 | + let active = self.niri.layout.active_output().cloned(); | ||
| 257 | + if let Some(active) = active { | ||
| 258 | + if let Some(renderer) = self.backend.renderer() { | ||
| 259 | + if let Err(err) = self.niri.screenshot(renderer, &active) { | ||
| 260 | + warn!("error taking screenshot: {err:?}"); | ||
| 173 | } | 261 | } |
| 174 | } | 262 | } |
| 175 | - Action::ScreenshotWindow => { | 263 | + } |
| 176 | - let active = self.niri.layout.active_window(); | 264 | + } |
| 177 | - if let Some((window, output)) = active { | 265 | + Action::ConfirmScreenshot => { |
| 178 | - if let Some(renderer) = self.backend.renderer() { | 266 | + if let Some(renderer) = self.backend.renderer() { |
| 179 | - if let Err(err) = | 267 | + match self.niri.screenshot_ui.capture(renderer) { |
| 180 | - self.niri.screenshot_window(renderer, &output, &window) | 268 | + Ok((size, pixels)) => { |
| 181 | - { | 269 | + if let Err(err) = self.niri.save_screenshot(size, pixels) { |
| 182 | - warn!("error taking screenshot: {err:?}"); | 270 | + warn!("error saving screenshot: {err:?}"); |
| 183 | - } | ||
| 184 | } | 271 | } |
| 185 | } | 272 | } |
| 186 | - } | 273 | + Err(err) => { |
| 187 | - Action::CloseWindow => { | 274 | + warn!("error capturing screenshot: {err:?}"); |
| 188 | - if let Some(window) = self.niri.layout.focus() { | ||
| 189 | - window.toplevel().send_close(); | ||
| 190 | } | 275 | } |
| 191 | } | 276 | } |
| 192 | - Action::FullscreenWindow => { | 277 | + } |
| 193 | - let focus = self.niri.layout.focus().cloned(); | 278 | + |
| 194 | - if let Some(window) = focus { | 279 | + self.niri.screenshot_ui.close(); |
| 195 | - self.niri.layout.toggle_fullscreen(&window); | 280 | + self.niri |
| 196 | - } | 281 | + .cursor_manager |
| 197 | - } | 282 | + .set_cursor_image(CursorImageStatus::default_named()); |
| 198 | - Action::SwitchLayout(action) => { | 283 | + self.niri.queue_redraw_all(); |
| 199 | - self.niri | 284 | + } |
| 200 | - .seat | 285 | + Action::CancelScreenshot => { |
| 201 | - .get_keyboard() | 286 | + self.niri.screenshot_ui.close(); |
| 202 | - .unwrap() | 287 | + self.niri |
| 203 | - .with_xkb_state(self, |mut state| match action { | 288 | + .cursor_manager |
| 204 | - LayoutAction::Next => state.cycle_next_layout(), | 289 | + .set_cursor_image(CursorImageStatus::default_named()); |
| 205 | - LayoutAction::Prev => state.cycle_prev_layout(), | 290 | + self.niri.queue_redraw_all(); |
| 206 | - }); | 291 | + } |
| 207 | - } | 292 | + Action::Screenshot => { |
| 208 | - Action::MoveColumnLeft => { | 293 | + if let Some(renderer) = self.backend.renderer() { |
| 209 | - self.niri.layout.move_left(); | 294 | + self.niri.open_screenshot_ui(renderer); |
| 210 | - // FIXME: granular | 295 | + } |
| 211 | - self.niri.queue_redraw_all(); | 296 | + } |
| 212 | - } | 297 | + Action::ScreenshotWindow => { |
| 213 | - Action::MoveColumnRight => { | 298 | + let active = self.niri.layout.active_window(); |
| 214 | - self.niri.layout.move_right(); | 299 | + if let Some((window, output)) = active { |
| 215 | - // FIXME: granular | 300 | + if let Some(renderer) = self.backend.renderer() { |
| 216 | - self.niri.queue_redraw_all(); | 301 | + if let Err(err) = self.niri.screenshot_window(renderer, &output, &window) { |
| 217 | - } | 302 | + warn!("error taking screenshot: {err:?}"); |
| 218 | - Action::MoveWindowDown => { | ||
| 219 | - self.niri.layout.move_down(); | ||
| 220 | - // FIXME: granular | ||
| 221 | - self.niri.queue_redraw_all(); | ||
| 222 | - } | ||
| 223 | - Action::MoveWindowUp => { | ||
| 224 | - self.niri.layout.move_up(); | ||
| 225 | - // FIXME: granular | ||
| 226 | - self.niri.queue_redraw_all(); | ||
| 227 | - } | ||
| 228 | - Action::FocusColumnLeft => { | ||
| 229 | - self.niri.layout.focus_left(); | ||
| 230 | - } | ||
| 231 | - Action::FocusColumnRight => { | ||
| 232 | - self.niri.layout.focus_right(); | ||
| 233 | - } | ||
| 234 | - Action::FocusWindowDown => { | ||
| 235 | - self.niri.layout.focus_down(); | ||
| 236 | - } | ||
| 237 | - Action::FocusWindowUp => { | ||
| 238 | - self.niri.layout.focus_up(); | ||
| 239 | - } | ||
| 240 | - Action::MoveWindowToWorkspaceDown => { | ||
| 241 | - self.niri.layout.move_to_workspace_down(); | ||
| 242 | - // FIXME: granular | ||
| 243 | - self.niri.queue_redraw_all(); | ||
| 244 | - } | ||
| 245 | - Action::MoveWindowToWorkspaceUp => { | ||
| 246 | - self.niri.layout.move_to_workspace_up(); | ||
| 247 | - // FIXME: granular | ||
| 248 | - self.niri.queue_redraw_all(); | ||
| 249 | - } | ||
| 250 | - Action::MoveWindowToWorkspace(idx) => { | ||
| 251 | - let idx = idx.saturating_sub(1) as usize; | ||
| 252 | - self.niri.layout.move_to_workspace(idx); | ||
| 253 | - // FIXME: granular | ||
| 254 | - self.niri.queue_redraw_all(); | ||
| 255 | - } | ||
| 256 | - Action::FocusWorkspaceDown => { | ||
| 257 | - self.niri.layout.switch_workspace_down(); | ||
| 258 | - // FIXME: granular | ||
| 259 | - self.niri.queue_redraw_all(); | ||
| 260 | - } | ||
| 261 | - Action::FocusWorkspaceUp => { | ||
| 262 | - self.niri.layout.switch_workspace_up(); | ||
| 263 | - // FIXME: granular | ||
| 264 | - self.niri.queue_redraw_all(); | ||
| 265 | - } | ||
| 266 | - Action::FocusWorkspace(idx) => { | ||
| 267 | - let idx = idx.saturating_sub(1) as usize; | ||
| 268 | - self.niri.layout.switch_workspace(idx); | ||
| 269 | - // FIXME: granular | ||
| 270 | - self.niri.queue_redraw_all(); | ||
| 271 | - } | ||
| 272 | - Action::MoveWorkspaceDown => { | ||
| 273 | - self.niri.layout.move_workspace_down(); | ||
| 274 | - // FIXME: granular | ||
| 275 | - self.niri.queue_redraw_all(); | ||
| 276 | - } | ||
| 277 | - Action::MoveWorkspaceUp => { | ||
| 278 | - self.niri.layout.move_workspace_up(); | ||
| 279 | - // FIXME: granular | ||
| 280 | - self.niri.queue_redraw_all(); | ||
| 281 | - } | ||
| 282 | - Action::ConsumeWindowIntoColumn => { | ||
| 283 | - self.niri.layout.consume_into_column(); | ||
| 284 | - // FIXME: granular | ||
| 285 | - self.niri.queue_redraw_all(); | ||
| 286 | - } | ||
| 287 | - Action::ExpelWindowFromColumn => { | ||
| 288 | - self.niri.layout.expel_from_column(); | ||
| 289 | - // FIXME: granular | ||
| 290 | - self.niri.queue_redraw_all(); | ||
| 291 | - } | ||
| 292 | - Action::SwitchPresetColumnWidth => { | ||
| 293 | - self.niri.layout.toggle_width(); | ||
| 294 | - } | ||
| 295 | - Action::CenterColumn => { | ||
| 296 | - self.niri.layout.center_column(); | ||
| 297 | - // FIXME: granular | ||
| 298 | - self.niri.queue_redraw_all(); | ||
| 299 | - } | ||
| 300 | - Action::MaximizeColumn => { | ||
| 301 | - self.niri.layout.toggle_full_width(); | ||
| 302 | - } | ||
| 303 | - Action::FocusMonitorLeft => { | ||
| 304 | - if let Some(output) = self.niri.output_left() { | ||
| 305 | - self.niri.layout.focus_output(&output); | ||
| 306 | - self.move_cursor_to_output(&output); | ||
| 307 | - } | ||
| 308 | - } | ||
| 309 | - Action::FocusMonitorRight => { | ||
| 310 | - if let Some(output) = self.niri.output_right() { | ||
| 311 | - self.niri.layout.focus_output(&output); | ||
| 312 | - self.move_cursor_to_output(&output); | ||
| 313 | - } | ||
| 314 | - } | ||
| 315 | - Action::FocusMonitorDown => { | ||
| 316 | - if let Some(output) = self.niri.output_down() { | ||
| 317 | - self.niri.layout.focus_output(&output); | ||
| 318 | - self.move_cursor_to_output(&output); | ||
| 319 | - } | ||
| 320 | - } | ||
| 321 | - Action::FocusMonitorUp => { | ||
| 322 | - if let Some(output) = self.niri.output_up() { | ||
| 323 | - self.niri.layout.focus_output(&output); | ||
| 324 | - self.move_cursor_to_output(&output); | ||
| 325 | - } | ||
| 326 | - } | ||
| 327 | - Action::MoveWindowToMonitorLeft => { | ||
| 328 | - if let Some(output) = self.niri.output_left() { | ||
| 329 | - self.niri.layout.move_to_output(&output); | ||
| 330 | - self.move_cursor_to_output(&output); | ||
| 331 | - } | ||
| 332 | - } | ||
| 333 | - Action::MoveWindowToMonitorRight => { | ||
| 334 | - if let Some(output) = self.niri.output_right() { | ||
| 335 | - self.niri.layout.move_to_output(&output); | ||
| 336 | - self.move_cursor_to_output(&output); | ||
| 337 | - } | ||
| 338 | - } | ||
| 339 | - Action::MoveWindowToMonitorDown => { | ||
| 340 | - if let Some(output) = self.niri.output_down() { | ||
| 341 | - self.niri.layout.move_to_output(&output); | ||
| 342 | - self.move_cursor_to_output(&output); | ||
| 343 | - } | ||
| 344 | - } | ||
| 345 | - Action::MoveWindowToMonitorUp => { | ||
| 346 | - if let Some(output) = self.niri.output_up() { | ||
| 347 | - self.niri.layout.move_to_output(&output); | ||
| 348 | - self.move_cursor_to_output(&output); | ||
| 349 | } | 303 | } |
| 350 | } | 304 | } |
| 351 | - Action::SetColumnWidth(change) => { | ||
| 352 | - self.niri.layout.set_column_width(change); | ||
| 353 | - } | ||
| 354 | - Action::SetWindowHeight(change) => { | ||
| 355 | - self.niri.layout.set_window_height(change); | ||
| 356 | - } | ||
| 357 | } | 305 | } |
| 358 | } | 306 | } |
| 359 | - InputEvent::PointerMotion { event, .. } => { | 307 | + Action::CloseWindow => { |
| 360 | - // We need an output to be able to move the pointer. | 308 | + if let Some(window) = self.niri.layout.focus() { |
| 361 | - if self.niri.global_space.outputs().next().is_none() { | 309 | + window.toplevel().send_close(); |
| 362 | - return; | ||
| 363 | } | 310 | } |
| 311 | + } | ||
| 312 | + Action::FullscreenWindow => { | ||
| 313 | + let focus = self.niri.layout.focus().cloned(); | ||
| 314 | + if let Some(window) = focus { | ||
| 315 | + self.niri.layout.toggle_fullscreen(&window); | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + Action::SwitchLayout(action) => { | ||
| 319 | + self.niri.seat.get_keyboard().unwrap().with_xkb_state( | ||
| 320 | + self, | ||
| 321 | + |mut state| match action { | ||
| 322 | + LayoutAction::Next => state.cycle_next_layout(), | ||
| 323 | + LayoutAction::Prev => state.cycle_prev_layout(), | ||
| 324 | + }, | ||
| 325 | + ); | ||
| 326 | + } | ||
| 327 | + Action::MoveColumnLeft => { | ||
| 328 | + self.niri.layout.move_left(); | ||
| 329 | + // FIXME: granular | ||
| 330 | + self.niri.queue_redraw_all(); | ||
| 331 | + } | ||
| 332 | + Action::MoveColumnRight => { | ||
| 333 | + self.niri.layout.move_right(); | ||
| 334 | + // FIXME: granular | ||
| 335 | + self.niri.queue_redraw_all(); | ||
| 336 | + } | ||
| 337 | + Action::MoveWindowDown => { | ||
| 338 | + self.niri.layout.move_down(); | ||
| 339 | + // FIXME: granular | ||
| 340 | + self.niri.queue_redraw_all(); | ||
| 341 | + } | ||
| 342 | + Action::MoveWindowUp => { | ||
| 343 | + self.niri.layout.move_up(); | ||
| 344 | + // FIXME: granular | ||
| 345 | + self.niri.queue_redraw_all(); | ||
| 346 | + } | ||
| 347 | + Action::FocusColumnLeft => { | ||
| 348 | + self.niri.layout.focus_left(); | ||
| 349 | + } | ||
| 350 | + Action::FocusColumnRight => { | ||
| 351 | + self.niri.layout.focus_right(); | ||
| 352 | + } | ||
| 353 | + Action::FocusWindowDown => { | ||
| 354 | + self.niri.layout.focus_down(); | ||
| 355 | + } | ||
| 356 | + Action::FocusWindowUp => { | ||
| 357 | + self.niri.layout.focus_up(); | ||
| 358 | + } | ||
| 359 | + Action::MoveWindowToWorkspaceDown => { | ||
| 360 | + self.niri.layout.move_to_workspace_down(); | ||
| 361 | + // FIXME: granular | ||
| 362 | + self.niri.queue_redraw_all(); | ||
| 363 | + } | ||
| 364 | + Action::MoveWindowToWorkspaceUp => { | ||
| 365 | + self.niri.layout.move_to_workspace_up(); | ||
| 366 | + // FIXME: granular | ||
| 367 | + self.niri.queue_redraw_all(); | ||
| 368 | + } | ||
| 369 | + Action::MoveWindowToWorkspace(idx) => { | ||
| 370 | + let idx = idx.saturating_sub(1) as usize; | ||
| 371 | + self.niri.layout.move_to_workspace(idx); | ||
| 372 | + // FIXME: granular | ||
| 373 | + self.niri.queue_redraw_all(); | ||
| 374 | + } | ||
| 375 | + Action::FocusWorkspaceDown => { | ||
| 376 | + self.niri.layout.switch_workspace_down(); | ||
| 377 | + // FIXME: granular | ||
| 378 | + self.niri.queue_redraw_all(); | ||
| 379 | + } | ||
| 380 | + Action::FocusWorkspaceUp => { | ||
| 381 | + self.niri.layout.switch_workspace_up(); | ||
| 382 | + // FIXME: granular | ||
| 383 | + self.niri.queue_redraw_all(); | ||
| 384 | + } | ||
| 385 | + Action::FocusWorkspace(idx) => { | ||
| 386 | + let idx = idx.saturating_sub(1) as usize; | ||
| 387 | + self.niri.layout.switch_workspace(idx); | ||
| 388 | + // FIXME: granular | ||
| 389 | + self.niri.queue_redraw_all(); | ||
| 390 | + } | ||
| 391 | + Action::MoveWorkspaceDown => { | ||
| 392 | + self.niri.layout.move_workspace_down(); | ||
| 393 | + // FIXME: granular | ||
| 394 | + self.niri.queue_redraw_all(); | ||
| 395 | + } | ||
| 396 | + Action::MoveWorkspaceUp => { | ||
| 397 | + self.niri.layout.move_workspace_up(); | ||
| 398 | + // FIXME: granular | ||
| 399 | + self.niri.queue_redraw_all(); | ||
| 400 | + } | ||
| 401 | + Action::ConsumeWindowIntoColumn => { | ||
| 402 | + self.niri.layout.consume_into_column(); | ||
| 403 | + // FIXME: granular | ||
| 404 | + self.niri.queue_redraw_all(); | ||
| 405 | + } | ||
| 406 | + Action::ExpelWindowFromColumn => { | ||
| 407 | + self.niri.layout.expel_from_column(); | ||
| 408 | + // FIXME: granular | ||
| 409 | + self.niri.queue_redraw_all(); | ||
| 410 | + } | ||
| 411 | + Action::SwitchPresetColumnWidth => { | ||
| 412 | + self.niri.layout.toggle_width(); | ||
| 413 | + } | ||
| 414 | + Action::CenterColumn => { | ||
| 415 | + self.niri.layout.center_column(); | ||
| 416 | + // FIXME: granular | ||
| 417 | + self.niri.queue_redraw_all(); | ||
| 418 | + } | ||
| 419 | + Action::MaximizeColumn => { | ||
| 420 | + self.niri.layout.toggle_full_width(); | ||
| 421 | + } | ||
| 422 | + Action::FocusMonitorLeft => { | ||
| 423 | + if let Some(output) = self.niri.output_left() { | ||
| 424 | + self.niri.layout.focus_output(&output); | ||
| 425 | + self.move_cursor_to_output(&output); | ||
| 426 | + } | ||
| 427 | + } | ||
| 428 | + Action::FocusMonitorRight => { | ||
| 429 | + if let Some(output) = self.niri.output_right() { | ||
| 430 | + self.niri.layout.focus_output(&output); | ||
| 431 | + self.move_cursor_to_output(&output); | ||
| 432 | + } | ||
| 433 | + } | ||
| 434 | + Action::FocusMonitorDown => { | ||
| 435 | + if let Some(output) = self.niri.output_down() { | ||
| 436 | + self.niri.layout.focus_output(&output); | ||
| 437 | + self.move_cursor_to_output(&output); | ||
| 438 | + } | ||
| 439 | + } | ||
| 440 | + Action::FocusMonitorUp => { | ||
| 441 | + if let Some(output) = self.niri.output_up() { | ||
| 442 | + self.niri.layout.focus_output(&output); | ||
| 443 | + self.move_cursor_to_output(&output); | ||
| 444 | + } | ||
| 445 | + } | ||
| 446 | + Action::MoveWindowToMonitorLeft => { | ||
| 447 | + if let Some(output) = self.niri.output_left() { | ||
| 448 | + self.niri.layout.move_to_output(&output); | ||
| 449 | + self.move_cursor_to_output(&output); | ||
| 450 | + } | ||
| 451 | + } | ||
| 452 | + Action::MoveWindowToMonitorRight => { | ||
| 453 | + if let Some(output) = self.niri.output_right() { | ||
| 454 | + self.niri.layout.move_to_output(&output); | ||
| 455 | + self.move_cursor_to_output(&output); | ||
| 456 | + } | ||
| 457 | + } | ||
| 458 | + Action::MoveWindowToMonitorDown => { | ||
| 459 | + if let Some(output) = self.niri.output_down() { | ||
| 460 | + self.niri.layout.move_to_output(&output); | ||
| 461 | + self.move_cursor_to_output(&output); | ||
| 462 | + } | ||
| 463 | + } | ||
| 464 | + Action::MoveWindowToMonitorUp => { | ||
| 465 | + if let Some(output) = self.niri.output_up() { | ||
| 466 | + self.niri.layout.move_to_output(&output); | ||
| 467 | + self.move_cursor_to_output(&output); | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | + Action::SetColumnWidth(change) => { | ||
| 471 | + self.niri.layout.set_column_width(change); | ||
| 472 | + } | ||
| 473 | + Action::SetWindowHeight(change) => { | ||
| 474 | + self.niri.layout.set_window_height(change); | ||
| 475 | + } | ||
| 476 | + } | ||
| 477 | + } | ||
| 364 | 478 | ||
| 365 | - let serial = SERIAL_COUNTER.next_serial(); | 479 | + fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) { |
| 480 | + // We need an output to be able to move the pointer. | ||
| 481 | + if self.niri.global_space.outputs().next().is_none() { | ||
| 482 | + return; | ||
| 483 | + } | ||
| 366 | 484 | ||
| 367 | - let pointer = self.niri.seat.get_pointer().unwrap(); | 485 | + let serial = SERIAL_COUNTER.next_serial(); |
| 486 | + | ||
| 487 | + let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 488 | + | ||
| 489 | + let pos = pointer.current_location(); | ||
| 490 | + | ||
| 491 | + // We have an output, so we can compute the new location and focus. | ||
| 492 | + let mut new_pos = pos + event.delta(); | ||
| 493 | + | ||
| 494 | + if self | ||
| 495 | + .niri | ||
| 496 | + .global_space | ||
| 497 | + .output_under(new_pos) | ||
| 498 | + .next() | ||
| 499 | + .is_none() | ||
| 500 | + { | ||
| 501 | + // We ended up outside the outputs and need to clip the movement. | ||
| 502 | + if let Some(output) = self.niri.global_space.output_under(pos).next() { | ||
| 503 | + // The pointer was previously on some output. Clip the movement against its | ||
| 504 | + // boundaries. | ||
| 505 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 506 | + new_pos.x = new_pos | ||
| 507 | + .x | ||
| 508 | + .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 509 | + new_pos.y = new_pos | ||
| 510 | + .y | ||
| 511 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 512 | + } else { | ||
| 513 | + // The pointer was not on any output in the first place. Find one for it. | ||
| 514 | + // Let's do the simple thing and just put it on the first output. | ||
| 515 | + let output = self.niri.global_space.outputs().next().unwrap(); | ||
| 516 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 517 | + new_pos = center(geom).to_f64(); | ||
| 518 | + } | ||
| 519 | + } | ||
| 368 | 520 | ||
| 369 | - let pos = pointer.current_location(); | 521 | + if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 522 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 523 | + let mut point = new_pos; | ||
| 524 | + point.x = point | ||
| 525 | + .x | ||
| 526 | + .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 527 | + point.y = point | ||
| 528 | + .y | ||
| 529 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 530 | + let point = (point - geom.loc.to_f64()) | ||
| 531 | + .to_physical(output.current_scale().fractional_scale()) | ||
| 532 | + .to_i32_round(); | ||
| 533 | + self.niri.screenshot_ui.pointer_motion(point); | ||
| 534 | + } | ||
| 370 | 535 | ||
| 371 | - // We have an output, so we can compute the new location and focus. | 536 | + let under = self.niri.surface_under_and_global_space(new_pos); |
| 372 | - let mut new_pos = pos + event.delta(); | 537 | + self.niri.pointer_focus = under.clone(); |
| 538 | + let under = under.map(|u| u.surface); | ||
| 539 | + | ||
| 540 | + pointer.motion( | ||
| 541 | + self, | ||
| 542 | + under.clone(), | ||
| 543 | + &MotionEvent { | ||
| 544 | + location: new_pos, | ||
| 545 | + serial, | ||
| 546 | + time: event.time_msec(), | ||
| 547 | + }, | ||
| 548 | + ); | ||
| 549 | + | ||
| 550 | + pointer.relative_motion( | ||
| 551 | + self, | ||
| 552 | + under, | ||
| 553 | + &RelativeMotionEvent { | ||
| 554 | + delta: event.delta(), | ||
| 555 | + delta_unaccel: event.delta_unaccel(), | ||
| 556 | + utime: event.time(), | ||
| 557 | + }, | ||
| 558 | + ); | ||
| 373 | 559 | ||
| 374 | - if self | 560 | + pointer.frame(self); |
| 375 | - .niri | ||
| 376 | - .global_space | ||
| 377 | - .output_under(new_pos) | ||
| 378 | - .next() | ||
| 379 | - .is_none() | ||
| 380 | - { | ||
| 381 | - // We ended up outside the outputs and need to clip the movement. | ||
| 382 | - if let Some(output) = self.niri.global_space.output_under(pos).next() { | ||
| 383 | - // The pointer was previously on some output. Clip the movement against its | ||
| 384 | - // boundaries. | ||
| 385 | - let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 386 | - new_pos.x = new_pos | ||
| 387 | - .x | ||
| 388 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 389 | - new_pos.y = new_pos | ||
| 390 | - .y | ||
| 391 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 392 | - } else { | ||
| 393 | - // The pointer was not on any output in the first place. Find one for it. | ||
| 394 | - // Let's do the simple thing and just put it on the first output. | ||
| 395 | - let output = self.niri.global_space.outputs().next().unwrap(); | ||
| 396 | - let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 397 | - new_pos = center(geom).to_f64(); | ||
| 398 | - } | ||
| 399 | - } | ||
| 400 | 561 | ||
| 401 | - if let Some(output) = self.niri.screenshot_ui.selection_output() { | 562 | + // We moved the regular pointer, so show it now. |
| 402 | - let geom = self.niri.global_space.output_geometry(output).unwrap(); | 563 | + self.niri.tablet_cursor_location = None; |
| 403 | - let mut point = new_pos; | ||
| 404 | - point.x = point | ||
| 405 | - .x | ||
| 406 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 407 | - point.y = point | ||
| 408 | - .y | ||
| 409 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 410 | - let point = (point - geom.loc.to_f64()) | ||
| 411 | - .to_physical(output.current_scale().fractional_scale()) | ||
| 412 | - .to_i32_round(); | ||
| 413 | - self.niri.screenshot_ui.pointer_motion(point); | ||
| 414 | - } | ||
| 415 | 564 | ||
| 416 | - let under = self.niri.surface_under_and_global_space(new_pos); | 565 | + // Redraw to update the cursor position. |
| 417 | - self.niri.pointer_focus = under.clone(); | 566 | + // FIXME: redraw only outputs overlapping the cursor. |
| 418 | - let under = under.map(|u| u.surface); | 567 | + self.niri.queue_redraw_all(); |
| 568 | + } | ||
| 419 | 569 | ||
| 420 | - pointer.motion( | 570 | + fn on_pointer_motion_absolute<I: InputBackend>( |
| 421 | - self, | 571 | + &mut self, |
| 422 | - under.clone(), | 572 | + event: I::PointerMotionAbsoluteEvent, |
| 423 | - &MotionEvent { | 573 | + ) { |
| 424 | - location: new_pos, | 574 | + let Some(output) = self.niri.global_space.outputs().next() else { |
| 425 | - serial, | 575 | + return; |
| 426 | - time: event.time_msec(), | 576 | + }; |
| 427 | - }, | ||
| 428 | - ); | ||
| 429 | 577 | ||
| 430 | - pointer.relative_motion( | 578 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); |
| 431 | - self, | ||
| 432 | - under, | ||
| 433 | - &RelativeMotionEvent { | ||
| 434 | - delta: event.delta(), | ||
| 435 | - delta_unaccel: event.delta_unaccel(), | ||
| 436 | - utime: event.time(), | ||
| 437 | - }, | ||
| 438 | - ); | ||
| 439 | 579 | ||
| 440 | - pointer.frame(self); | 580 | + let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); |
| 581 | + | ||
| 582 | + let serial = SERIAL_COUNTER.next_serial(); | ||
| 583 | + | ||
| 584 | + let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 585 | + | ||
| 586 | + if let Some(output) = self.niri.screenshot_ui.selection_output() { | ||
| 587 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 588 | + let mut point = pos; | ||
| 589 | + point.x = point | ||
| 590 | + .x | ||
| 591 | + .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 592 | + point.y = point | ||
| 593 | + .y | ||
| 594 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 595 | + let point = (point - geom.loc.to_f64()) | ||
| 596 | + .to_physical(output.current_scale().fractional_scale()) | ||
| 597 | + .to_i32_round(); | ||
| 598 | + self.niri.screenshot_ui.pointer_motion(point); | ||
| 599 | + } | ||
| 441 | 600 | ||
| 442 | - // We moved the regular pointer, so show it now. | 601 | + let under = self.niri.surface_under_and_global_space(pos); |
| 443 | - self.niri.tablet_cursor_location = None; | 602 | + self.niri.pointer_focus = under.clone(); |
| 603 | + let under = under.map(|u| u.surface); | ||
| 604 | + | ||
| 605 | + pointer.motion( | ||
| 606 | + self, | ||
| 607 | + under, | ||
| 608 | + &MotionEvent { | ||
| 609 | + location: pos, | ||
| 610 | + serial, | ||
| 611 | + time: event.time_msec(), | ||
| 612 | + }, | ||
| 613 | + ); | ||
| 444 | 614 | ||
| 445 | - // Redraw to update the cursor position. | 615 | + pointer.frame(self); |
| 446 | - // FIXME: redraw only outputs overlapping the cursor. | ||
| 447 | - self.niri.queue_redraw_all(); | ||
| 448 | - } | ||
| 449 | - InputEvent::PointerMotionAbsolute { event, .. } => { | ||
| 450 | - let Some(output) = self.niri.global_space.outputs().next() else { | ||
| 451 | - return; | ||
| 452 | - }; | ||
| 453 | 616 | ||
| 454 | - let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | 617 | + // We moved the regular pointer, so show it now. |
| 618 | + self.niri.tablet_cursor_location = None; | ||
| 455 | 619 | ||
| 456 | - let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); | 620 | + // Redraw to update the cursor position. |
| 621 | + // FIXME: redraw only outputs overlapping the cursor. | ||
| 622 | + self.niri.queue_redraw_all(); | ||
| 623 | + } | ||
| 457 | 624 | ||
| 458 | - let serial = SERIAL_COUNTER.next_serial(); | 625 | + fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) { |
| 626 | + let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 459 | 627 | ||
| 460 | - let pointer = self.niri.seat.get_pointer().unwrap(); | 628 | + let serial = SERIAL_COUNTER.next_serial(); |
| 461 | 629 | ||
| 462 | - if let Some(output) = self.niri.screenshot_ui.selection_output() { | 630 | + let button = event.button_code(); |
| 463 | - let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 464 | - let mut point = pos; | ||
| 465 | - point.x = point | ||
| 466 | - .x | ||
| 467 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 468 | - point.y = point | ||
| 469 | - .y | ||
| 470 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 471 | - let point = (point - geom.loc.to_f64()) | ||
| 472 | - .to_physical(output.current_scale().fractional_scale()) | ||
| 473 | - .to_i32_round(); | ||
| 474 | - self.niri.screenshot_ui.pointer_motion(point); | ||
| 475 | - } | ||
| 476 | 631 | ||
| 477 | - let under = self.niri.surface_under_and_global_space(pos); | 632 | + let button_state = event.state(); |
| 478 | - self.niri.pointer_focus = under.clone(); | ||
| 479 | - let under = under.map(|u| u.surface); | ||
| 480 | 633 | ||
| 481 | - pointer.motion( | 634 | + if ButtonState::Pressed == button_state && !pointer.is_grabbed() { |
| 482 | - self, | 635 | + if let Some(window) = self.niri.window_under_cursor() { |
| 483 | - under, | 636 | + let window = window.clone(); |
| 484 | - &MotionEvent { | 637 | + self.niri.layout.activate_window(&window); |
| 485 | - location: pos, | 638 | + } else if let Some(output) = self.niri.output_under_cursor() { |
| 486 | - serial, | 639 | + self.niri.layout.activate_output(&output); |
| 487 | - time: event.time_msec(), | 640 | + } |
| 488 | - }, | 641 | + }; |
| 489 | - ); | ||
| 490 | 642 | ||
| 491 | - pointer.frame(self); | 643 | + self.update_pointer_focus(); |
| 644 | + | ||
| 645 | + if let Some(button) = event.button() { | ||
| 646 | + let pos = pointer.current_location(); | ||
| 647 | + if let Some((output, _)) = self.niri.output_under(pos) { | ||
| 648 | + let output = output.clone(); | ||
| 649 | + let geom = self.niri.global_space.output_geometry(&output).unwrap(); | ||
| 650 | + let mut point = pos; | ||
| 651 | + // Re-clamp as pointer can be within 0.5 from the limit which will round up | ||
| 652 | + // to a wrong value. | ||
| 653 | + point.x = point | ||
| 654 | + .x | ||
| 655 | + .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 656 | + point.y = point | ||
| 657 | + .y | ||
| 658 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 659 | + let point = (point - geom.loc.to_f64()) | ||
| 660 | + .to_physical(output.current_scale().fractional_scale()) | ||
| 661 | + .to_i32_round(); | ||
| 662 | + if self | ||
| 663 | + .niri | ||
| 664 | + .screenshot_ui | ||
| 665 | + .pointer_button(output, point, button, button_state) | ||
| 666 | + { | ||
| 667 | + self.niri.queue_redraw_all(); | ||
| 668 | + } | ||
| 669 | + } | ||
| 670 | + } | ||
| 492 | 671 | ||
| 493 | - // We moved the regular pointer, so show it now. | 672 | + pointer.button( |
| 494 | - self.niri.tablet_cursor_location = None; | 673 | + self, |
| 674 | + &ButtonEvent { | ||
| 675 | + button, | ||
| 676 | + state: button_state, | ||
| 677 | + serial, | ||
| 678 | + time: event.time_msec(), | ||
| 679 | + }, | ||
| 680 | + ); | ||
| 681 | + pointer.frame(self); | ||
| 682 | + } | ||
| 495 | 683 | ||
| 496 | - // Redraw to update the cursor position. | 684 | + fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) { |
| 497 | - // FIXME: redraw only outputs overlapping the cursor. | 685 | + let source = event.source(); |
| 498 | - self.niri.queue_redraw_all(); | 686 | + |
| 687 | + let horizontal_amount = event | ||
| 688 | + .amount(Axis::Horizontal) | ||
| 689 | + .unwrap_or_else(|| event.amount_discrete(Axis::Horizontal).unwrap_or(0.0) * 3.0); | ||
| 690 | + let vertical_amount = event | ||
| 691 | + .amount(Axis::Vertical) | ||
| 692 | + .unwrap_or_else(|| event.amount_discrete(Axis::Vertical).unwrap_or(0.0) * 3.0); | ||
| 693 | + let horizontal_amount_discrete = event.amount_discrete(Axis::Horizontal); | ||
| 694 | + let vertical_amount_discrete = event.amount_discrete(Axis::Vertical); | ||
| 695 | + | ||
| 696 | + let mut frame = AxisFrame::new(event.time_msec()).source(source); | ||
| 697 | + if horizontal_amount != 0.0 { | ||
| 698 | + frame = frame.value(Axis::Horizontal, horizontal_amount); | ||
| 699 | + if let Some(discrete) = horizontal_amount_discrete { | ||
| 700 | + frame = frame.discrete(Axis::Horizontal, discrete as i32); | ||
| 701 | + } | ||
| 702 | + } | ||
| 703 | + if vertical_amount != 0.0 { | ||
| 704 | + frame = frame.value(Axis::Vertical, vertical_amount); | ||
| 705 | + if let Some(discrete) = vertical_amount_discrete { | ||
| 706 | + frame = frame.discrete(Axis::Vertical, discrete as i32); | ||
| 499 | } | 707 | } |
| 500 | - InputEvent::PointerButton { event, .. } => { | 708 | + } |
| 501 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 502 | 709 | ||
| 503 | - let serial = SERIAL_COUNTER.next_serial(); | 710 | + if source == AxisSource::Finger { |
| 711 | + if event.amount(Axis::Horizontal) == Some(0.0) { | ||
| 712 | + frame = frame.stop(Axis::Horizontal); | ||
| 713 | + } | ||
| 714 | + if event.amount(Axis::Vertical) == Some(0.0) { | ||
| 715 | + frame = frame.stop(Axis::Vertical); | ||
| 716 | + } | ||
| 717 | + } | ||
| 504 | 718 | ||
| 505 | - let button = event.button_code(); | 719 | + self.update_pointer_focus(); |
| 506 | 720 | ||
| 507 | - let button_state = event.state(); | 721 | + let pointer = &self.niri.seat.get_pointer().unwrap(); |
| 722 | + pointer.axis(self, frame); | ||
| 723 | + pointer.frame(self); | ||
| 724 | + } | ||
| 508 | 725 | ||
| 509 | - if ButtonState::Pressed == button_state && !pointer.is_grabbed() { | 726 | + fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent) |
| 510 | - if let Some(window) = self.niri.window_under_cursor() { | 727 | + where |
| 511 | - let window = window.clone(); | 728 | + I::Device: 'static, // Needed for downcasting. |
| 512 | - self.niri.layout.activate_window(&window); | 729 | + { |
| 513 | - } else if let Some(output) = self.niri.output_under_cursor() { | 730 | + let Some(pos) = self.compute_tablet_position(&event) else { |
| 514 | - self.niri.layout.activate_output(&output); | 731 | + return; |
| 515 | - } | 732 | + }; |
| 516 | - }; | ||
| 517 | - | ||
| 518 | - self.update_pointer_focus(); | ||
| 519 | - | ||
| 520 | - if let Some(button) = event.button() { | ||
| 521 | - let pos = pointer.current_location(); | ||
| 522 | - if let Some((output, _)) = self.niri.output_under(pos) { | ||
| 523 | - let output = output.clone(); | ||
| 524 | - let geom = self.niri.global_space.output_geometry(&output).unwrap(); | ||
| 525 | - let mut point = pos; | ||
| 526 | - // Re-clamp as pointer can be within 0.5 from the limit which will round up | ||
| 527 | - // to a wrong value. | ||
| 528 | - point.x = point | ||
| 529 | - .x | ||
| 530 | - .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 531 | - point.y = point | ||
| 532 | - .y | ||
| 533 | - .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 534 | - let point = (point - geom.loc.to_f64()) | ||
| 535 | - .to_physical(output.current_scale().fractional_scale()) | ||
| 536 | - .to_i32_round(); | ||
| 537 | - if self.niri.screenshot_ui.pointer_button( | ||
| 538 | - output, | ||
| 539 | - point, | ||
| 540 | - button, | ||
| 541 | - button_state, | ||
| 542 | - ) { | ||
| 543 | - self.niri.queue_redraw_all(); | ||
| 544 | - } | ||
| 545 | - } | ||
| 546 | - } | ||
| 547 | 733 | ||
| 548 | - pointer.button( | 734 | + let under = self.niri.surface_under_and_global_space(pos); |
| 549 | - self, | 735 | + let under = under.map(|u| u.surface); |
| 550 | - &ButtonEvent { | ||
| 551 | - button, | ||
| 552 | - state: button_state, | ||
| 553 | - serial, | ||
| 554 | - time: event.time_msec(), | ||
| 555 | - }, | ||
| 556 | - ); | ||
| 557 | - pointer.frame(self); | ||
| 558 | - } | ||
| 559 | - InputEvent::PointerAxis { event, .. } => { | ||
| 560 | - let source = event.source(); | ||
| 561 | - | ||
| 562 | - let horizontal_amount = event.amount(Axis::Horizontal).unwrap_or_else(|| { | ||
| 563 | - event.amount_discrete(Axis::Horizontal).unwrap_or(0.0) * 3.0 | ||
| 564 | - }); | ||
| 565 | - let vertical_amount = event | ||
| 566 | - .amount(Axis::Vertical) | ||
| 567 | - .unwrap_or_else(|| event.amount_discrete(Axis::Vertical).unwrap_or(0.0) * 3.0); | ||
| 568 | - let horizontal_amount_discrete = event.amount_discrete(Axis::Horizontal); | ||
| 569 | - let vertical_amount_discrete = event.amount_discrete(Axis::Vertical); | ||
| 570 | - | ||
| 571 | - let mut frame = AxisFrame::new(event.time_msec()).source(source); | ||
| 572 | - if horizontal_amount != 0.0 { | ||
| 573 | - frame = frame.value(Axis::Horizontal, horizontal_amount); | ||
| 574 | - if let Some(discrete) = horizontal_amount_discrete { | ||
| 575 | - frame = frame.discrete(Axis::Horizontal, discrete as i32); | ||
| 576 | - } | ||
| 577 | - } | ||
| 578 | - if vertical_amount != 0.0 { | ||
| 579 | - frame = frame.value(Axis::Vertical, vertical_amount); | ||
| 580 | - if let Some(discrete) = vertical_amount_discrete { | ||
| 581 | - frame = frame.discrete(Axis::Vertical, discrete as i32); | ||
| 582 | - } | ||
| 583 | - } | ||
| 584 | 736 | ||
| 585 | - if source == AxisSource::Finger { | 737 | + let tablet_seat = self.niri.seat.tablet_seat(); |
| 586 | - if event.amount(Axis::Horizontal) == Some(0.0) { | 738 | + let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| 587 | - frame = frame.stop(Axis::Horizontal); | 739 | + let tool = tablet_seat.get_tool(&event.tool()); |
| 588 | - } | 740 | + if let (Some(tablet), Some(tool)) = (tablet, tool) { |
| 589 | - if event.amount(Axis::Vertical) == Some(0.0) { | 741 | + if event.pressure_has_changed() { |
| 590 | - frame = frame.stop(Axis::Vertical); | 742 | + tool.pressure(event.pressure()); |
| 591 | - } | 743 | + } |
| 592 | - } | 744 | + if event.distance_has_changed() { |
| 745 | + tool.distance(event.distance()); | ||
| 746 | + } | ||
| 747 | + if event.tilt_has_changed() { | ||
| 748 | + tool.tilt(event.tilt()); | ||
| 749 | + } | ||
| 750 | + if event.slider_has_changed() { | ||
| 751 | + tool.slider_position(event.slider_position()); | ||
| 752 | + } | ||
| 753 | + if event.rotation_has_changed() { | ||
| 754 | + tool.rotation(event.rotation()); | ||
| 755 | + } | ||
| 756 | + if event.wheel_has_changed() { | ||
| 757 | + tool.wheel(event.wheel_delta(), event.wheel_delta_discrete()); | ||
| 758 | + } | ||
| 593 | 759 | ||
| 594 | - self.update_pointer_focus(); | 760 | + tool.motion( |
| 761 | + pos, | ||
| 762 | + under, | ||
| 763 | + &tablet, | ||
| 764 | + SERIAL_COUNTER.next_serial(), | ||
| 765 | + event.time_msec(), | ||
| 766 | + ); | ||
| 595 | 767 | ||
| 596 | - let pointer = &self.niri.seat.get_pointer().unwrap(); | 768 | + self.niri.tablet_cursor_location = Some(pos); |
| 597 | - pointer.axis(self, frame); | 769 | + } |
| 598 | - pointer.frame(self); | ||
| 599 | - } | ||
| 600 | - InputEvent::TabletToolAxis { event, .. } => { | ||
| 601 | - let Some(pos) = self.compute_tablet_position(&event) else { | ||
| 602 | - return; | ||
| 603 | - }; | ||
| 604 | 770 | ||
| 605 | - let under = self.niri.surface_under_and_global_space(pos); | 771 | + // Redraw to update the cursor position. |
| 606 | - let under = under.map(|u| u.surface); | 772 | + // FIXME: redraw only outputs overlapping the cursor. |
| 773 | + self.niri.queue_redraw_all(); | ||
| 774 | + } | ||
| 607 | 775 | ||
| 608 | - let tablet_seat = self.niri.seat.tablet_seat(); | 776 | + fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) { |
| 609 | - let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); | 777 | + let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 610 | - let tool = tablet_seat.get_tool(&event.tool()); | ||
| 611 | - if let (Some(tablet), Some(tool)) = (tablet, tool) { | ||
| 612 | - if event.pressure_has_changed() { | ||
| 613 | - tool.pressure(event.pressure()); | ||
| 614 | - } | ||
| 615 | - if event.distance_has_changed() { | ||
| 616 | - tool.distance(event.distance()); | ||
| 617 | - } | ||
| 618 | - if event.tilt_has_changed() { | ||
| 619 | - tool.tilt(event.tilt()); | ||
| 620 | - } | ||
| 621 | - if event.slider_has_changed() { | ||
| 622 | - tool.slider_position(event.slider_position()); | ||
| 623 | - } | ||
| 624 | - if event.rotation_has_changed() { | ||
| 625 | - tool.rotation(event.rotation()); | ||
| 626 | - } | ||
| 627 | - if event.wheel_has_changed() { | ||
| 628 | - tool.wheel(event.wheel_delta(), event.wheel_delta_discrete()); | ||
| 629 | - } | ||
| 630 | 778 | ||
| 631 | - tool.motion( | 779 | + if let Some(tool) = tool { |
| 632 | - pos, | 780 | + match event.tip_state() { |
| 633 | - under, | 781 | + TabletToolTipState::Down => { |
| 634 | - &tablet, | 782 | + let serial = SERIAL_COUNTER.next_serial(); |
| 635 | - SERIAL_COUNTER.next_serial(), | 783 | + tool.tip_down(serial, event.time_msec()); |
| 636 | - event.time_msec(), | ||
| 637 | - ); | ||
| 638 | 784 | ||
| 639 | - self.niri.tablet_cursor_location = Some(pos); | 785 | + if let Some(pos) = self.niri.tablet_cursor_location { |
| 640 | - } | 786 | + if let Some(window) = self.niri.window_under(pos) { |
| 787 | + let window = window.clone(); | ||
| 788 | + self.niri.layout.activate_window(&window); | ||
| 641 | 789 | ||
| 642 | - // Redraw to update the cursor position. | 790 | + // FIXME: granular. |
| 643 | - // FIXME: redraw only outputs overlapping the cursor. | 791 | + self.niri.queue_redraw_all(); |
| 644 | - self.niri.queue_redraw_all(); | 792 | + } else if let Some((output, _)) = self.niri.output_under(pos) { |
| 645 | - } | 793 | + let output = output.clone(); |
| 646 | - InputEvent::TabletToolTip { event, .. } => { | 794 | + self.niri.layout.activate_output(&output); |
| 647 | - let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); | ||
| 648 | - | ||
| 649 | - if let Some(tool) = tool { | ||
| 650 | - match event.tip_state() { | ||
| 651 | - TabletToolTipState::Down => { | ||
| 652 | - let serial = SERIAL_COUNTER.next_serial(); | ||
| 653 | - tool.tip_down(serial, event.time_msec()); | ||
| 654 | - | ||
| 655 | - if let Some(pos) = self.niri.tablet_cursor_location { | ||
| 656 | - if let Some(window) = self.niri.window_under(pos) { | ||
| 657 | - let window = window.clone(); | ||
| 658 | - self.niri.layout.activate_window(&window); | ||
| 659 | - | ||
| 660 | - // FIXME: granular. | ||
| 661 | - self.niri.queue_redraw_all(); | ||
| 662 | - } else if let Some((output, _)) = self.niri.output_under(pos) { | ||
| 663 | - let output = output.clone(); | ||
| 664 | - self.niri.layout.activate_output(&output); | ||
| 665 | - | ||
| 666 | - // FIXME: granular. | ||
| 667 | - self.niri.queue_redraw_all(); | ||
| 668 | - } | ||
| 669 | - } | ||
| 670 | - } | ||
| 671 | - TabletToolTipState::Up => { | ||
| 672 | - tool.tip_up(event.time_msec()); | ||
| 673 | - } | ||
| 674 | - } | ||
| 675 | - } | ||
| 676 | - } | ||
| 677 | - InputEvent::TabletToolProximity { event, .. } => { | ||
| 678 | - let Some(pos) = self.compute_tablet_position(&event) else { | ||
| 679 | - return; | ||
| 680 | - }; | ||
| 681 | - | ||
| 682 | - let under = self.niri.surface_under_and_global_space(pos); | ||
| 683 | - let under = under.map(|u| u.surface); | ||
| 684 | - | ||
| 685 | - let tablet_seat = self.niri.seat.tablet_seat(); | ||
| 686 | - let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool()); | ||
| 687 | - let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); | ||
| 688 | - if let Some(tablet) = tablet { | ||
| 689 | - match event.state() { | ||
| 690 | - ProximityState::In => { | ||
| 691 | - if let Some(under) = under { | ||
| 692 | - tool.proximity_in( | ||
| 693 | - pos, | ||
| 694 | - under, | ||
| 695 | - &tablet, | ||
| 696 | - SERIAL_COUNTER.next_serial(), | ||
| 697 | - event.time_msec(), | ||
| 698 | - ); | ||
| 699 | - } | ||
| 700 | - self.niri.tablet_cursor_location = Some(pos); | ||
| 701 | - } | ||
| 702 | - ProximityState::Out => { | ||
| 703 | - tool.proximity_out(event.time_msec()); | ||
| 704 | - | ||
| 705 | - // Move the mouse pointer here to avoid discontinuity. | ||
| 706 | - // | ||
| 707 | - // Plus, Wayland SDL2 currently warps the pointer into some weird | ||
| 708 | - // location on proximity out, so this shuold help it a little. | ||
| 709 | - if let Some(pos) = self.niri.tablet_cursor_location { | ||
| 710 | - self.move_cursor(pos); | ||
| 711 | - } | ||
| 712 | 795 | ||
| 713 | - self.niri.tablet_cursor_location = None; | 796 | + // FIXME: granular. |
| 797 | + self.niri.queue_redraw_all(); | ||
| 714 | } | 798 | } |
| 715 | } | 799 | } |
| 716 | - | ||
| 717 | - // FIXME: granular. | ||
| 718 | - self.niri.queue_redraw_all(); | ||
| 719 | } | 800 | } |
| 720 | - } | 801 | + TabletToolTipState::Up => { |
| 721 | - InputEvent::TabletToolButton { event, .. } => { | 802 | + tool.tip_up(event.time_msec()); |
| 722 | - let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); | ||
| 723 | - | ||
| 724 | - if let Some(tool) = tool { | ||
| 725 | - tool.button( | ||
| 726 | - event.button(), | ||
| 727 | - event.button_state(), | ||
| 728 | - SERIAL_COUNTER.next_serial(), | ||
| 729 | - event.time_msec(), | ||
| 730 | - ); | ||
| 731 | } | 803 | } |
| 732 | } | 804 | } |
| 733 | - InputEvent::DeviceAdded { device } => { | 805 | + } |
| 734 | - if device.has_capability(DeviceCapability::TabletTool) { | 806 | + } |
| 735 | - self.niri.seat.tablet_seat().add_tablet::<Self>( | ||
| 736 | - &self.niri.display_handle, | ||
| 737 | - &TabletDescriptor::from(&device), | ||
| 738 | - ); | ||
| 739 | - } | ||
| 740 | - } | ||
| 741 | - InputEvent::DeviceRemoved { device } => { | ||
| 742 | - if device.has_capability(DeviceCapability::TabletTool) { | ||
| 743 | - let tablet_seat = self.niri.seat.tablet_seat(); | ||
| 744 | 807 | ||
| 745 | - tablet_seat.remove_tablet(&TabletDescriptor::from(&device)); | 808 | + fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent) |
| 809 | + where | ||
| 810 | + I::Device: 'static, // Needed for downcasting. | ||
| 811 | + { | ||
| 812 | + let Some(pos) = self.compute_tablet_position(&event) else { | ||
| 813 | + return; | ||
| 814 | + }; | ||
| 746 | 815 | ||
| 747 | - // If there are no tablets in seat we can remove all tools | 816 | + let under = self.niri.surface_under_and_global_space(pos); |
| 748 | - if tablet_seat.count_tablets() == 0 { | 817 | + let under = under.map(|u| u.surface); |
| 749 | - tablet_seat.clear_tools(); | 818 | + |
| 819 | + let tablet_seat = self.niri.seat.tablet_seat(); | ||
| 820 | + let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool()); | ||
| 821 | + let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); | ||
| 822 | + if let Some(tablet) = tablet { | ||
| 823 | + match event.state() { | ||
| 824 | + ProximityState::In => { | ||
| 825 | + if let Some(under) = under { | ||
| 826 | + tool.proximity_in( | ||
| 827 | + pos, | ||
| 828 | + under, | ||
| 829 | + &tablet, | ||
| 830 | + SERIAL_COUNTER.next_serial(), | ||
| 831 | + event.time_msec(), | ||
| 832 | + ); | ||
| 750 | } | 833 | } |
| 834 | + self.niri.tablet_cursor_location = Some(pos); | ||
| 751 | } | 835 | } |
| 752 | - } | 836 | + ProximityState::Out => { |
| 753 | - InputEvent::GestureSwipeBegin { event } => { | 837 | + tool.proximity_out(event.time_msec()); |
| 754 | - if event.fingers() == 3 { | ||
| 755 | - if let Some(output) = self.niri.output_under_cursor() { | ||
| 756 | - self.niri.layout.workspace_switch_gesture_begin(&output); | ||
| 757 | 838 | ||
| 758 | - // FIXME: granular. This one is awkward because this can cancel a gesture on | 839 | + // Move the mouse pointer here to avoid discontinuity. |
| 759 | - // multiple other outputs in theory. | 840 | + // |
| 760 | - self.niri.queue_redraw_all(); | 841 | + // Plus, Wayland SDL2 currently warps the pointer into some weird |
| 842 | + // location on proximity out, so this shuold help it a little. | ||
| 843 | + if let Some(pos) = self.niri.tablet_cursor_location { | ||
| 844 | + self.move_cursor(pos); | ||
| 761 | } | 845 | } |
| 762 | 846 | ||
| 763 | - // We handled this event. | 847 | + self.niri.tablet_cursor_location = None; |
| 764 | - return; | ||
| 765 | } | 848 | } |
| 849 | + } | ||
| 766 | 850 | ||
| 767 | - let serial = SERIAL_COUNTER.next_serial(); | 851 | + // FIXME: granular. |
| 768 | - let pointer = self.niri.seat.get_pointer().unwrap(); | 852 | + self.niri.queue_redraw_all(); |
| 853 | + } | ||
| 854 | + } | ||
| 769 | 855 | ||
| 770 | - if self.update_pointer_focus() { | 856 | + fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) { |
| 771 | - pointer.frame(self); | 857 | + let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 772 | - } | ||
| 773 | 858 | ||
| 774 | - pointer.gesture_swipe_begin( | 859 | + if let Some(tool) = tool { |
| 775 | - self, | 860 | + tool.button( |
| 776 | - &GestureSwipeBeginEvent { | 861 | + event.button(), |
| 777 | - serial, | 862 | + event.button_state(), |
| 778 | - time: event.time_msec(), | 863 | + SERIAL_COUNTER.next_serial(), |
| 779 | - fingers: event.fingers(), | 864 | + event.time_msec(), |
| 780 | - }, | 865 | + ); |
| 781 | - ); | 866 | + } |
| 867 | + } | ||
| 868 | + | ||
| 869 | + fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) { | ||
| 870 | + if event.fingers() == 3 { | ||
| 871 | + if let Some(output) = self.niri.output_under_cursor() { | ||
| 872 | + self.niri.layout.workspace_switch_gesture_begin(&output); | ||
| 873 | + | ||
| 874 | + // FIXME: granular. This one is awkward because this can cancel a gesture on | ||
| 875 | + // multiple other outputs in theory. | ||
| 876 | + self.niri.queue_redraw_all(); | ||
| 782 | } | 877 | } |
| 783 | - InputEvent::GestureSwipeUpdate { event } => { | ||
| 784 | - let res = self | ||
| 785 | - .niri | ||
| 786 | - .layout | ||
| 787 | - .workspace_switch_gesture_update(event.delta_y()); | ||
| 788 | - if let Some(output) = res { | ||
| 789 | - if let Some(output) = output { | ||
| 790 | - self.niri.queue_redraw(output); | ||
| 791 | - } | ||
| 792 | 878 | ||
| 793 | - // We handled this event. | 879 | + // We handled this event. |
| 794 | - return; | 880 | + return; |
| 795 | - } | 881 | + } |
| 796 | 882 | ||
| 797 | - let pointer = self.niri.seat.get_pointer().unwrap(); | 883 | + let serial = SERIAL_COUNTER.next_serial(); |
| 884 | + let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 798 | 885 | ||
| 799 | - if self.update_pointer_focus() { | 886 | + if self.update_pointer_focus() { |
| 800 | - pointer.frame(self); | 887 | + pointer.frame(self); |
| 801 | - } | 888 | + } |
| 802 | 889 | ||
| 803 | - pointer.gesture_swipe_update( | 890 | + pointer.gesture_swipe_begin( |
| 804 | - self, | 891 | + self, |
| 805 | - &GestureSwipeUpdateEvent { | 892 | + &GestureSwipeBeginEvent { |
| 806 | - time: event.time_msec(), | 893 | + serial, |
| 807 | - delta: event.delta(), | 894 | + time: event.time_msec(), |
| 808 | - }, | 895 | + fingers: event.fingers(), |
| 809 | - ); | 896 | + }, |
| 897 | + ); | ||
| 898 | + } | ||
| 899 | + | ||
| 900 | + fn on_gesture_swipe_update<I: InputBackend>(&mut self, event: I::GestureSwipeUpdateEvent) { | ||
| 901 | + let res = self | ||
| 902 | + .niri | ||
| 903 | + .layout | ||
| 904 | + .workspace_switch_gesture_update(event.delta_y()); | ||
| 905 | + if let Some(output) = res { | ||
| 906 | + if let Some(output) = output { | ||
| 907 | + self.niri.queue_redraw(output); | ||
| 810 | } | 908 | } |
| 811 | - InputEvent::GestureSwipeEnd { event } => { | ||
| 812 | - let res = self | ||
| 813 | - .niri | ||
| 814 | - .layout | ||
| 815 | - .workspace_switch_gesture_end(event.cancelled()); | ||
| 816 | - if let Some(output) = res { | ||
| 817 | - self.niri.queue_redraw(output); | ||
| 818 | 909 | ||
| 819 | - // We handled this event. | 910 | + // We handled this event. |
| 820 | - return; | 911 | + return; |
| 821 | - } | 912 | + } |
| 822 | 913 | ||
| 823 | - let serial = SERIAL_COUNTER.next_serial(); | 914 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 824 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 825 | 915 | ||
| 826 | - if self.update_pointer_focus() { | 916 | + if self.update_pointer_focus() { |
| 827 | - pointer.frame(self); | 917 | + pointer.frame(self); |
| 828 | - } | 918 | + } |
| 829 | 919 | ||
| 830 | - pointer.gesture_swipe_end( | 920 | + pointer.gesture_swipe_update( |
| 831 | - self, | 921 | + self, |
| 832 | - &GestureSwipeEndEvent { | 922 | + &GestureSwipeUpdateEvent { |
| 833 | - serial, | 923 | + time: event.time_msec(), |
| 834 | - time: event.time_msec(), | 924 | + delta: event.delta(), |
| 835 | - cancelled: event.cancelled(), | 925 | + }, |
| 836 | - }, | 926 | + ); |
| 837 | - ); | 927 | + } |
| 838 | - } | ||
| 839 | - InputEvent::GesturePinchBegin { event } => { | ||
| 840 | - let serial = SERIAL_COUNTER.next_serial(); | ||
| 841 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 842 | 928 | ||
| 843 | - if self.update_pointer_focus() { | 929 | + fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) { |
| 844 | - pointer.frame(self); | 930 | + let res = self |
| 845 | - } | 931 | + .niri |
| 932 | + .layout | ||
| 933 | + .workspace_switch_gesture_end(event.cancelled()); | ||
| 934 | + if let Some(output) = res { | ||
| 935 | + self.niri.queue_redraw(output); | ||
| 846 | 936 | ||
| 847 | - pointer.gesture_pinch_begin( | 937 | + // We handled this event. |
| 848 | - self, | 938 | + return; |
| 849 | - &GesturePinchBeginEvent { | 939 | + } |
| 850 | - serial, | ||
| 851 | - time: event.time_msec(), | ||
| 852 | - fingers: event.fingers(), | ||
| 853 | - }, | ||
| 854 | - ); | ||
| 855 | - } | ||
| 856 | - InputEvent::GesturePinchUpdate { event } => { | ||
| 857 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 858 | 940 | ||
| 859 | - if self.update_pointer_focus() { | 941 | + let serial = SERIAL_COUNTER.next_serial(); |
| 860 | - pointer.frame(self); | 942 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 861 | - } | ||
| 862 | 943 | ||
| 863 | - pointer.gesture_pinch_update( | 944 | + if self.update_pointer_focus() { |
| 864 | - self, | 945 | + pointer.frame(self); |
| 865 | - &GesturePinchUpdateEvent { | 946 | + } |
| 866 | - time: event.time_msec(), | ||
| 867 | - delta: event.delta(), | ||
| 868 | - scale: event.scale(), | ||
| 869 | - rotation: event.rotation(), | ||
| 870 | - }, | ||
| 871 | - ); | ||
| 872 | - } | ||
| 873 | - InputEvent::GesturePinchEnd { event } => { | ||
| 874 | - let serial = SERIAL_COUNTER.next_serial(); | ||
| 875 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 876 | 947 | ||
| 877 | - if self.update_pointer_focus() { | 948 | + pointer.gesture_swipe_end( |
| 878 | - pointer.frame(self); | 949 | + self, |
| 879 | - } | 950 | + &GestureSwipeEndEvent { |
| 951 | + serial, | ||
| 952 | + time: event.time_msec(), | ||
| 953 | + cancelled: event.cancelled(), | ||
| 954 | + }, | ||
| 955 | + ); | ||
| 956 | + } | ||
| 880 | 957 | ||
| 881 | - pointer.gesture_pinch_end( | 958 | + fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) { |
| 882 | - self, | 959 | + let serial = SERIAL_COUNTER.next_serial(); |
| 883 | - &GesturePinchEndEvent { | 960 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 884 | - serial, | ||
| 885 | - time: event.time_msec(), | ||
| 886 | - cancelled: event.cancelled(), | ||
| 887 | - }, | ||
| 888 | - ); | ||
| 889 | - } | ||
| 890 | - InputEvent::GestureHoldBegin { event } => { | ||
| 891 | - let serial = SERIAL_COUNTER.next_serial(); | ||
| 892 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 893 | 961 | ||
| 894 | - if self.update_pointer_focus() { | 962 | + if self.update_pointer_focus() { |
| 895 | - pointer.frame(self); | 963 | + pointer.frame(self); |
| 896 | - } | 964 | + } |
| 897 | 965 | ||
| 898 | - pointer.gesture_hold_begin( | 966 | + pointer.gesture_pinch_begin( |
| 899 | - self, | 967 | + self, |
| 900 | - &GestureHoldBeginEvent { | 968 | + &GesturePinchBeginEvent { |
| 901 | - serial, | 969 | + serial, |
| 902 | - time: event.time_msec(), | 970 | + time: event.time_msec(), |
| 903 | - fingers: event.fingers(), | 971 | + fingers: event.fingers(), |
| 904 | - }, | 972 | + }, |
| 905 | - ); | 973 | + ); |
| 906 | - } | 974 | + } |
| 907 | - InputEvent::GestureHoldEnd { event } => { | ||
| 908 | - let serial = SERIAL_COUNTER.next_serial(); | ||
| 909 | - let pointer = self.niri.seat.get_pointer().unwrap(); | ||
| 910 | 975 | ||
| 911 | - if self.update_pointer_focus() { | 976 | + fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) { |
| 912 | - pointer.frame(self); | 977 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 913 | - } | ||
| 914 | 978 | ||
| 915 | - pointer.gesture_hold_end( | 979 | + if self.update_pointer_focus() { |
| 916 | - self, | 980 | + pointer.frame(self); |
| 917 | - &GestureHoldEndEvent { | ||
| 918 | - serial, | ||
| 919 | - time: event.time_msec(), | ||
| 920 | - cancelled: event.cancelled(), | ||
| 921 | - }, | ||
| 922 | - ); | ||
| 923 | - } | ||
| 924 | - InputEvent::TouchDown { .. } => (), | ||
| 925 | - InputEvent::TouchMotion { .. } => (), | ||
| 926 | - InputEvent::TouchUp { .. } => (), | ||
| 927 | - InputEvent::TouchCancel { .. } => (), | ||
| 928 | - InputEvent::TouchFrame { .. } => (), | ||
| 929 | - InputEvent::Special(_) => (), | ||
| 930 | } | 981 | } |
| 982 | + | ||
| 983 | + pointer.gesture_pinch_update( | ||
| 984 | + self, | ||
| 985 | + &GesturePinchUpdateEvent { | ||
| 986 | + time: event.time_msec(), | ||
| 987 | + delta: event.delta(), | ||
| 988 | + scale: event.scale(), | ||
| 989 | + rotation: event.rotation(), | ||
| 990 | + }, | ||
| 991 | + ); | ||
| 931 | } | 992 | } |
| 932 | 993 | ||
| 933 | - pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { | 994 | + fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) { |
| 934 | - match event { | 995 | + let serial = SERIAL_COUNTER.next_serial(); |
| 935 | - InputEvent::DeviceAdded { device } => { | 996 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 936 | - // According to Mutter code, this setting is specific to touchpads. | ||
| 937 | - let is_touchpad = device.config_tap_finger_count() > 0; | ||
| 938 | - if is_touchpad { | ||
| 939 | - let c = &self.niri.config.borrow().input.touchpad; | ||
| 940 | - let _ = device.config_tap_set_enabled(c.tap); | ||
| 941 | - let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); | ||
| 942 | - let _ = device.config_accel_set_speed(c.accel_speed); | ||
| 943 | - } | ||
| 944 | 997 | ||
| 945 | - if device.has_capability(smithay::reexports::input::DeviceCapability::TabletTool) { | 998 | + if self.update_pointer_focus() { |
| 946 | - match device.size() { | 999 | + pointer.frame(self); |
| 947 | - Some((w, h)) => { | ||
| 948 | - self.niri.tablets.insert( | ||
| 949 | - device.clone(), | ||
| 950 | - TabletData { | ||
| 951 | - aspect_ratio: w / h, | ||
| 952 | - }, | ||
| 953 | - ); | ||
| 954 | - } | ||
| 955 | - None => { | ||
| 956 | - warn!("tablet tool device has no size"); | ||
| 957 | - } | ||
| 958 | - } | ||
| 959 | - } | ||
| 960 | - } | ||
| 961 | - InputEvent::DeviceRemoved { device } => { | ||
| 962 | - self.niri.tablets.remove(device); | ||
| 963 | - } | ||
| 964 | - _ => (), | ||
| 965 | } | 1000 | } |
| 1001 | + | ||
| 1002 | + pointer.gesture_pinch_end( | ||
| 1003 | + self, | ||
| 1004 | + &GesturePinchEndEvent { | ||
| 1005 | + serial, | ||
| 1006 | + time: event.time_msec(), | ||
| 1007 | + cancelled: event.cancelled(), | ||
| 1008 | + }, | ||
| 1009 | + ); | ||
| 966 | } | 1010 | } |
| 967 | 1011 | ||
| 968 | - fn compute_tablet_position<I: InputBackend>( | 1012 | + fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) { |
| 969 | - &mut self, | 1013 | + let serial = SERIAL_COUNTER.next_serial(); |
| 970 | - event: &(impl Event<I> + TabletToolEvent<I>), | 1014 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 971 | - ) -> Option<Point<f64, Logical>> | ||
| 972 | - where | ||
| 973 | - I::Device: 'static, | ||
| 974 | - { | ||
| 975 | - let output = self.niri.output_for_tablet()?; | ||
| 976 | - let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 977 | 1015 | ||
| 978 | - let mut pos = event.position_transformed(output_geo.size); | 1016 | + if self.update_pointer_focus() { |
| 979 | - pos.x /= output_geo.size.w as f64; | 1017 | + pointer.frame(self); |
| 980 | - pos.y /= output_geo.size.h as f64; | 1018 | + } |
| 981 | 1019 | ||
| 982 | - let device = event.device(); | 1020 | + pointer.gesture_hold_begin( |
| 983 | - if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { | 1021 | + self, |
| 984 | - if let Some(data) = self.niri.tablets.get(device) { | 1022 | + &GestureHoldBeginEvent { |
| 985 | - // This code does the same thing as mutter with "keep aspect ratio" enabled. | 1023 | + serial, |
| 986 | - let output_aspect_ratio = output_geo.size.w as f64 / output_geo.size.h as f64; | 1024 | + time: event.time_msec(), |
| 987 | - let ratio = data.aspect_ratio / output_aspect_ratio; | 1025 | + fingers: event.fingers(), |
| 1026 | + }, | ||
| 1027 | + ); | ||
| 1028 | + } | ||
| 988 | 1029 | ||
| 989 | - if ratio > 1. { | 1030 | + fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) { |
| 990 | - pos.x *= ratio; | 1031 | + let serial = SERIAL_COUNTER.next_serial(); |
| 991 | - } else { | 1032 | + let pointer = self.niri.seat.get_pointer().unwrap(); |
| 992 | - pos.y /= ratio; | ||
| 993 | - } | ||
| 994 | - } | ||
| 995 | - }; | ||
| 996 | 1033 | ||
| 997 | - pos.x *= output_geo.size.w as f64; | 1034 | + if self.update_pointer_focus() { |
| 998 | - pos.y *= output_geo.size.h as f64; | 1035 | + pointer.frame(self); |
| 999 | - pos.x = pos.x.clamp(0.0, output_geo.size.w as f64 - 1.); | 1036 | + } |
| 1000 | - pos.y = pos.y.clamp(0.0, output_geo.size.h as f64 - 1.); | 1037 | + |
| 1001 | - Some(pos + output_geo.loc.to_f64()) | 1038 | + pointer.gesture_hold_end( |
| 1039 | + self, | ||
| 1040 | + &GestureHoldEndEvent { | ||
| 1041 | + serial, | ||
| 1042 | + time: event.time_msec(), | ||
| 1043 | + cancelled: event.cancelled(), | ||
| 1044 | + }, | ||
| 1045 | + ); | ||
| 1002 | } | 1046 | } |
| 1003 | } | 1047 | } |
| 1004 | 1048 | ||