forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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