niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use niri_ipc::PickedColor; |
| 2 | use smithay::backend::allocator::Fourcc; |
| 3 | use smithay::backend::input::ButtonState; |
| 4 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 5 | use smithay::backend::renderer::ExportMem as _; |
| 6 | use smithay::input::pointer::{ |
| 7 | AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent, |
| 8 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, |
| 9 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, GrabStartData as PointerGrabStartData, |
| 10 | MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent, |
| 11 | }; |
| 12 | use smithay::input::SeatHandler; |
| 13 | use smithay::utils::{Logical, Physical, Point, Scale, Size, Transform}; |
| 14 | |
| 15 | use crate::niri::State; |
| 16 | use crate::render_helpers::{render_and_download, RenderCtx, RenderTarget}; |
| 17 | |
| 18 | pub struct PickColorGrab { |
| 19 | start_data: PointerGrabStartData<State>, |
| 20 | } |
| 21 | |
| 22 | impl PickColorGrab { |
| 23 | pub fn new(start_data: PointerGrabStartData<State>) -> Self { |
| 24 | Self { start_data } |
| 25 | } |
| 26 | |
| 27 | fn on_ungrab(&mut self, state: &mut State) { |
| 28 | if let Some(tx) = state.niri.pick_color.take() { |
| 29 | let _ = tx.send_blocking(None); |
| 30 | } |
| 31 | state |
| 32 | .niri |
| 33 | .cursor_manager |
| 34 | .set_cursor_image(CursorImageStatus::default_named()); |
| 35 | state.niri.queue_redraw_all(); |
| 36 | } |
| 37 | |
| 38 | fn pick_color_at_point(location: Point<f64, Logical>, data: &mut State) -> Option<PickedColor> { |
| 39 | let (output, pos_within_output) = data.niri.output_under(location)?; |
| 40 | let output = output.clone(); |
| 41 | |
| 42 | data.backend |
| 43 | .with_primary_renderer(|renderer| { |
| 44 | data.niri.update_render_elements(Some(&output)); |
| 45 | |
| 46 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 47 | // FIXME: perhaps replace floor with round once we figure out the pointer behavior |
| 48 | // at the bottom/right edges of the monitors. |
| 49 | let pos = pos_within_output.to_physical_precise_floor(scale); |
| 50 | let size = Size::<i32, Physical>::from((1, 1)); |
| 51 | |
| 52 | let ctx = RenderCtx { |
| 53 | renderer, |
| 54 | // This is an interactive operation so we can render without blocking out. |
| 55 | target: RenderTarget::Output, |
| 56 | xray: None, |
| 57 | }; |
| 58 | let elements = data.niri.render_to_vec(ctx, &output, false); |
| 59 | |
| 60 | let mapping = match render_and_download( |
| 61 | renderer, |
| 62 | size, |
| 63 | scale, |
| 64 | Transform::Normal, |
| 65 | Fourcc::Abgr8888, |
| 66 | elements.iter().rev().map(|elem| { |
| 67 | let offset = pos.upscale(-1); |
| 68 | RelocateRenderElement::from_element(elem, offset, Relocate::Relative) |
| 69 | }), |
| 70 | ) { |
| 71 | Ok(mapping) => mapping, |
| 72 | Err(_) => return None, |
| 73 | }; |
| 74 | let pixels = match renderer.map_texture(&mapping) { |
| 75 | Ok(pixels) => pixels, |
| 76 | Err(_) => return None, |
| 77 | }; |
| 78 | |
| 79 | if pixels.len() == 4 { |
| 80 | let rgb = [ |
| 81 | f64::from(pixels[0]) / 255.0, |
| 82 | f64::from(pixels[1]) / 255.0, |
| 83 | f64::from(pixels[2]) / 255.0, |
| 84 | ]; |
| 85 | Some(PickedColor { rgb }) |
| 86 | } else { |
| 87 | error!( |
| 88 | "unexpected pixel data length: {} (expected 4)", |
| 89 | pixels.len() |
| 90 | ); |
| 91 | None |
| 92 | } |
| 93 | }) |
| 94 | .flatten() |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl PointerGrab<State> for PickColorGrab { |
| 99 | fn motion( |
| 100 | &mut self, |
| 101 | data: &mut State, |
| 102 | handle: &mut PointerInnerHandle<'_, State>, |
| 103 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 104 | event: &MotionEvent, |
| 105 | ) { |
| 106 | handle.motion(data, None, event); |
| 107 | } |
| 108 | |
| 109 | fn relative_motion( |
| 110 | &mut self, |
| 111 | data: &mut State, |
| 112 | handle: &mut PointerInnerHandle<'_, State>, |
| 113 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 114 | event: &RelativeMotionEvent, |
| 115 | ) { |
| 116 | handle.relative_motion(data, None, event); |
| 117 | } |
| 118 | |
| 119 | fn button( |
| 120 | &mut self, |
| 121 | data: &mut State, |
| 122 | handle: &mut PointerInnerHandle<'_, State>, |
| 123 | event: &ButtonEvent, |
| 124 | ) { |
| 125 | if event.state != ButtonState::Pressed { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // We're handling this press, don't send the release to the window. |
| 130 | data.niri.suppressed_buttons.insert(event.button); |
| 131 | |
| 132 | if let Some(tx) = data.niri.pick_color.take() { |
| 133 | let color = Self::pick_color_at_point(handle.current_location(), data); |
| 134 | let _ = tx.send_blocking(color); |
| 135 | } |
| 136 | |
| 137 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 138 | } |
| 139 | |
| 140 | fn axis( |
| 141 | &mut self, |
| 142 | data: &mut State, |
| 143 | handle: &mut PointerInnerHandle<'_, State>, |
| 144 | details: AxisFrame, |
| 145 | ) { |
| 146 | handle.axis(data, details); |
| 147 | } |
| 148 | |
| 149 | fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) { |
| 150 | handle.frame(data); |
| 151 | } |
| 152 | |
| 153 | fn gesture_swipe_begin( |
| 154 | &mut self, |
| 155 | data: &mut State, |
| 156 | handle: &mut PointerInnerHandle<'_, State>, |
| 157 | event: &GestureSwipeBeginEvent, |
| 158 | ) { |
| 159 | handle.gesture_swipe_begin(data, event); |
| 160 | } |
| 161 | |
| 162 | fn gesture_swipe_update( |
| 163 | &mut self, |
| 164 | data: &mut State, |
| 165 | handle: &mut PointerInnerHandle<'_, State>, |
| 166 | event: &GestureSwipeUpdateEvent, |
| 167 | ) { |
| 168 | handle.gesture_swipe_update(data, event); |
| 169 | } |
| 170 | |
| 171 | fn gesture_swipe_end( |
| 172 | &mut self, |
| 173 | data: &mut State, |
| 174 | handle: &mut PointerInnerHandle<'_, State>, |
| 175 | event: &GestureSwipeEndEvent, |
| 176 | ) { |
| 177 | handle.gesture_swipe_end(data, event); |
| 178 | } |
| 179 | |
| 180 | fn gesture_pinch_begin( |
| 181 | &mut self, |
| 182 | data: &mut State, |
| 183 | handle: &mut PointerInnerHandle<'_, State>, |
| 184 | event: &GesturePinchBeginEvent, |
| 185 | ) { |
| 186 | handle.gesture_pinch_begin(data, event); |
| 187 | } |
| 188 | |
| 189 | fn gesture_pinch_update( |
| 190 | &mut self, |
| 191 | data: &mut State, |
| 192 | handle: &mut PointerInnerHandle<'_, State>, |
| 193 | event: &GesturePinchUpdateEvent, |
| 194 | ) { |
| 195 | handle.gesture_pinch_update(data, event); |
| 196 | } |
| 197 | |
| 198 | fn gesture_pinch_end( |
| 199 | &mut self, |
| 200 | data: &mut State, |
| 201 | handle: &mut PointerInnerHandle<'_, State>, |
| 202 | event: &GesturePinchEndEvent, |
| 203 | ) { |
| 204 | handle.gesture_pinch_end(data, event); |
| 205 | } |
| 206 | |
| 207 | fn gesture_hold_begin( |
| 208 | &mut self, |
| 209 | data: &mut State, |
| 210 | handle: &mut PointerInnerHandle<'_, State>, |
| 211 | event: &GestureHoldBeginEvent, |
| 212 | ) { |
| 213 | handle.gesture_hold_begin(data, event); |
| 214 | } |
| 215 | |
| 216 | fn gesture_hold_end( |
| 217 | &mut self, |
| 218 | data: &mut State, |
| 219 | handle: &mut PointerInnerHandle<'_, State>, |
| 220 | event: &GestureHoldEndEvent, |
| 221 | ) { |
| 222 | handle.gesture_hold_end(data, event); |
| 223 | } |
| 224 | |
| 225 | fn start_data(&self) -> &PointerGrabStartData<State> { |
| 226 | &self.start_data |
| 227 | } |
| 228 | |
| 229 | fn unset(&mut self, data: &mut State) { |
| 230 | self.on_ungrab(data); |
| 231 | } |
| 232 | } |