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