1use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
9use zx;
10
11pub fn duplicate_wake_lease(
13 wake_lease: Option<&fidl_next::wire::fuchsia::EventPair>,
14) -> Option<zx::EventPair> {
15 wake_lease.map(|h| h.as_raw_handle()).map(|raw| {
16 let unowned = unsafe { zx::Unowned::<'_, zx::EventPair>::from_raw_handle(raw) };
17 unowned.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("failed to duplicate wake lease")
18 })
19}
20
21pub fn duplicate_view_ref_next(
22 view_ref: &fidl_next_fuchsia_ui_views::ViewRef,
23) -> Result<fidl_next_fuchsia_ui_views::ViewRef, fidl::Status> {
24 let handle = view_ref.reference.as_handle_ref().duplicate_handle(fidl::Rights::SAME_RIGHTS)?;
25 Ok(fidl_next_fuchsia_ui_views::ViewRef { reference: handle.into() })
26}
27
28pub fn axis_to_next(
29 axis: Option<&fidl_fuchsia_input_report::Axis>,
30) -> Option<fidl_next_fuchsia_input_report::Axis> {
31 axis.map(|a| fidl_next_fuchsia_input_report::Axis {
32 range: fidl_next_fuchsia_input_report::Range { min: a.range.min, max: a.range.max },
33 unit: fidl_next_fuchsia_input_report::Unit {
34 type_: a.unit.type_.into_primitive().into(),
35 exponent: a.unit.exponent,
36 },
37 })
38}
39
40pub fn axis_to_old(
41 axis: Option<&fidl_next_fuchsia_input_report::Axis>,
42) -> Option<fidl_fuchsia_input_report::Axis> {
43 axis.map(|a| fidl_fuchsia_input_report::Axis {
44 range: fidl_fuchsia_input_report::Range { min: a.range.min, max: a.range.max },
45 unit: unit_to_old(&a.unit),
46 })
47}
48
49pub fn viewport_to_next(
50 viewport: &fidl_fuchsia_ui_pointerinjector::Viewport,
51) -> fidl_next_fuchsia_ui_pointerinjector::Viewport {
52 fidl_next_fuchsia_ui_pointerinjector::Viewport {
53 extents: viewport.extents.clone(),
54 viewport_to_context_transform: viewport.viewport_to_context_transform.clone(),
55 }
56}
57
58pub fn range_to_old(
59 range: &fidl_next_fuchsia_input_report::Range,
60) -> fidl_fuchsia_input_report::Range {
61 fidl_fuchsia_input_report::Range { min: range.min, max: range.max }
62}
63
64pub fn unit_to_old(unit: &fidl_next_fuchsia_input_report::Unit) -> fidl_fuchsia_input_report::Unit {
65 let discriminant: u32 =
66 unsafe { *(&unit.type_ as *const fidl_next_fuchsia_input_report::UnitType as *const u32) };
67 fidl_fuchsia_input_report::Unit {
68 type_: fidl_fuchsia_input_report::UnitType::from_primitive_allow_unknown(discriminant),
69 exponent: unit.exponent,
70 }
71}
72
73pub fn key_to_old(key: &fidl_next_fuchsia_input::Key) -> fidl_fuchsia_input::Key {
74 let discriminant: u32 = unsafe { *(key as *const fidl_next_fuchsia_input::Key as *const u32) };
75 fidl_fuchsia_input::Key::from_primitive_allow_unknown(discriminant)
76}
77
78#[cfg(test)]
79pub fn key_to_next(key: &fidl_fuchsia_input::Key) -> fidl_next_fuchsia_input::Key {
80 let discriminant = key.into_primitive();
81 fidl_next_fuchsia_input::Key::from(discriminant)
82}
83
84#[cfg(test)]
85pub fn touch_button_to_next(
86 button: &fidl_fuchsia_input_report::TouchButton,
87) -> fidl_next_fuchsia_input_report::TouchButton {
88 let discriminant = button.into_primitive();
89 fidl_next_fuchsia_input_report::TouchButton::from(discriminant)
90}
91
92pub fn consumer_control_button_to_old(
93 button: &fidl_next_fuchsia_input_report::ConsumerControlButton,
94) -> fidl_fuchsia_input_report::ConsumerControlButton {
95 let discriminant: u32 = unsafe {
96 *(button as *const fidl_next_fuchsia_input_report::ConsumerControlButton as *const u32)
97 };
98 fidl_fuchsia_input_report::ConsumerControlButton::from_primitive_allow_unknown(discriminant)
99}
100
101#[cfg(test)]
102pub fn consumer_control_button_to_next(
103 button: &fidl_fuchsia_input_report::ConsumerControlButton,
104) -> fidl_next_fuchsia_input_report::ConsumerControlButton {
105 let discriminant = button.into_primitive();
106 fidl_next_fuchsia_input_report::ConsumerControlButton::from(discriminant)
107}
108
109#[derive(Debug, PartialEq)]
111pub enum CursorMessage {
112 SetPosition(Position),
114 SetVisibility(bool),
116}
117
118#[derive(Clone, Copy, Debug, PartialEq)]
120pub struct Position {
121 pub x: f32,
123
124 pub y: f32,
126}
127
128impl Position {
129 pub fn clamp(target: &mut Position, min: Position, max: Position) {
130 if (*target).x < min.x {
131 (*target).x = min.x;
132 }
133 if (*target).x > max.x {
134 (*target).x = max.x;
135 }
136
137 if (*target).y < min.y {
138 (*target).y = min.y;
139 }
140 if (*target).y > max.y {
141 (*target).y = max.y;
142 }
143 }
144
145 pub fn clamp_size(target: &mut Position, min: Size, max: Size) {
146 if (*target).x < min.width {
147 (*target).x = min.width;
148 }
149 if (*target).x > max.width {
150 (*target).x = max.width;
151 }
152
153 if (*target).y < min.height {
154 (*target).y = min.height;
155 }
156 if (*target).y > max.height {
157 (*target).y = max.height;
158 }
159 }
160
161 pub fn zero() -> Position {
162 Position { x: 0.0, y: 0.0 }
163 }
164}
165
166impl Add for Position {
167 type Output = Self;
168
169 #[inline]
170 fn add(self, other: Self) -> Self {
171 Self { x: self.x + other.x, y: self.y + other.y }
172 }
173}
174
175impl AddAssign for Position {
176 #[inline]
177 fn add_assign(&mut self, other: Self) {
178 *self = Self { x: self.x + other.x, y: self.y + other.y };
179 }
180}
181
182impl Sub for Position {
183 type Output = Self;
184
185 #[inline]
186 fn sub(self, other: Self) -> Self {
187 Self { x: self.x - other.x, y: self.y - other.y }
188 }
189}
190
191impl SubAssign for Position {
192 #[inline]
193 fn sub_assign(&mut self, other: Self) {
194 *self = Self { x: self.x - other.x, y: self.y - other.y };
195 }
196}
197
198impl Div for Position {
199 type Output = Self;
200
201 #[inline]
202 fn div(self, other: Self) -> Self {
203 Self { x: self.x / other.x, y: self.y / other.y }
204 }
205}
206
207impl DivAssign for Position {
208 #[inline]
209 fn div_assign(&mut self, other: Self) {
210 *self = Self { x: self.x / other.x, y: self.y / other.y };
211 }
212}
213
214impl Mul for Position {
215 type Output = Self;
216
217 #[inline]
218 fn mul(self, other: Self) -> Self {
219 Self { x: self.x * other.x, y: self.y * other.y }
220 }
221}
222
223impl MulAssign for Position {
224 #[inline]
225 fn mul_assign(&mut self, other: Self) {
226 *self = Self { x: self.x * other.x, y: self.y * other.y };
227 }
228}
229
230impl Mul<Size> for Position {
231 type Output = Self;
232
233 #[inline]
234 fn mul(self, other: Size) -> Position {
235 Self { x: self.x * other.width, y: self.y * other.height }
236 }
237}
238
239macro_rules! scale_position_impl {
240 ($($t:ty)*) => ($(
241 impl Div<$t> for Position {
242 type Output = Position;
243
244 #[inline]
245 fn div(self, other: $t) -> Position {
246 Self { x: self.x / other as f32, y: self.y / other as f32 }
247 }
248 }
249
250 impl DivAssign<$t> for Position {
251 #[inline]
252 fn div_assign(&mut self, other: $t) {
253 *self = Self { x: self.x / other as f32, y: self.y / other as f32 };
254 }
255 }
256
257 impl Mul<$t> for Position {
258 type Output = Position;
259
260 #[inline]
261 fn mul(self, other: $t) -> Position {
262 Self { x: self.x * other as f32, y: self.y * other as f32 }
263 }
264 }
265
266 impl Mul<Position> for $t {
267 type Output = Position;
268
269 #[inline]
270 fn mul(self, other: Position) -> Position {
271 Position { x: self as f32 * other.x, y: self as f32 * other.y }
272 }
273 }
274
275 impl MulAssign<$t> for Position {
276 #[inline]
277 fn mul_assign(&mut self, other: $t) {
278 *self = Self { x: self.x * other as f32, y: self.y * other as f32 };
279 }
280 }
281 )*)
282}
283
284scale_position_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
285
286#[derive(Clone, Copy, Debug, PartialEq)]
288pub struct Size {
289 pub width: f32,
291
292 pub height: f32,
294}
295
296impl Size {
297 pub fn zero() -> Size {
298 Size { width: 0.0, height: 0.0 }
299 }
300}
301
302impl Add for Size {
303 type Output = Self;
304
305 #[inline]
306 fn add(self, other: Self) -> Self {
307 Self { width: self.width + other.width, height: self.height + other.height }
308 }
309}
310
311impl AddAssign for Size {
312 #[inline]
313 fn add_assign(&mut self, other: Self) {
314 *self = Self { width: self.width + other.width, height: self.height + other.height };
315 }
316}
317
318impl Sub for Size {
319 type Output = Self;
320
321 #[inline]
322 fn sub(self, other: Self) -> Self {
323 Self { width: self.width - other.width, height: self.height - other.height }
324 }
325}
326
327impl SubAssign for Size {
328 fn sub_assign(&mut self, other: Self) {
329 *self = Self { width: self.width - other.width, height: self.height - other.height };
330 }
331}
332
333impl Div for Size {
334 type Output = Self;
335
336 #[inline]
337 fn div(self, other: Self) -> Self {
338 Self { width: self.width / other.width, height: self.height / other.height }
339 }
340}
341
342impl DivAssign for Size {
343 #[inline]
344 fn div_assign(&mut self, other: Self) {
345 *self = Self { width: self.width / other.width, height: self.height / other.height };
346 }
347}
348
349impl Mul for Size {
350 type Output = Self;
351
352 #[inline]
353 fn mul(self, other: Self) -> Self {
354 Self { width: self.width * other.width, height: self.height * other.height }
355 }
356}
357
358impl MulAssign for Size {
359 #[inline]
360 fn mul_assign(&mut self, other: Self) {
361 *self = Self { width: self.width * other.width, height: self.height * other.height };
362 }
363}
364
365macro_rules! scale_size_impl {
366 ($($t:ty)*) => ($(
367 impl Div<$t> for Size {
368 type Output = Size;
369
370 #[inline]
371 fn div(self, other: $t) -> Size {
372 Self { width: self.width / other as f32, height: self.height / other as f32 }
373 }
374 }
375
376 impl DivAssign<$t> for Size {
377 #[inline]
378 fn div_assign(&mut self, other: $t) {
379 *self = Self { width: self.width / other as f32, height: self.height / other as f32 };
380 }
381 }
382
383 impl Mul<$t> for Size {
384 type Output = Size;
385
386 #[inline]
387 fn mul(self, other: $t) -> Size {
388 Self { width: self.width * other as f32, height: self.height * other as f32 }
389 }
390 }
391
392 impl MulAssign<$t> for Size {
393 #[inline]
394 fn mul_assign(&mut self, other: $t) {
395 *self = Self { width: self.width * other as f32, height: self.height * other as f32 };
396 }
397 }
398 )*)
399}
400
401scale_size_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }