gui_server_api/msg/
scalar.rs

1// SPDX-FileCopyrightText: 2024 Foundation Devices, Inc. <hello@foundation.xyz>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4use num_traits::{FromPrimitive, ToPrimitive};
5use server::{AsScalar, FromScalar};
6
7use crate::{GuiServerError, NextFrameAnimationKind};
8
9#[derive(Debug, server::Message)]
10pub struct SwitchTo {
11    pub next_pid: usize,
12    pub x: usize,
13    pub y: usize,
14}
15
16impl AsScalar<3> for SwitchTo {
17    fn as_scalar(&self) -> [u32; 3] { [self.next_pid as u32, self.x as u32, self.y as u32] }
18}
19
20impl FromScalar<3> for SwitchTo {
21    fn from_scalar([pid, x, y]: [u32; 3]) -> Self {
22        Self { next_pid: pid as usize, x: x as usize, y: y as usize }
23    }
24}
25
26#[derive(Debug, server::Message)]
27pub struct RequestRedraw;
28
29#[derive(
30    Debug,
31    PartialEq,
32    num_derive::FromPrimitive,
33    num_derive::ToPrimitive,
34    Copy,
35    Clone,
36    rkyv::Archive,
37    rkyv::Serialize,
38    rkyv::Deserialize,
39)]
40pub enum AppTheme {
41    System,
42    Dark,
43    Light,
44}
45
46impl server::AsScalar<1> for AppTheme {
47    fn as_scalar(&self) -> [u32; 1] { [self.to_u32().unwrap_or(0)] }
48}
49
50impl server::FromScalar<1> for AppTheme {
51    fn from_scalar([value]: [u32; 1]) -> Self { Self::from_u32(value).unwrap_or(AppTheme::System) }
52}
53
54#[derive(Debug, server::Message)]
55#[response(())]
56pub struct Shutdown {
57    pub reboot: bool,
58}
59
60impl FromScalar<1> for Shutdown {
61    fn from_scalar(value: [u32; 1]) -> Self { Self { reboot: bool::from_scalar(value) } }
62}
63
64impl AsScalar<1> for Shutdown {
65    fn as_scalar(&self) -> [u32; 1] { bool::as_scalar(&self.reboot) }
66}
67
68#[derive(Debug, server::Message)]
69#[response(bool)]
70pub struct SwitchToLauncher;
71
72#[derive(Debug, server::Message)]
73#[response(bool)]
74pub struct IsLocked;
75
76#[derive(Debug, server::Message)]
77#[response(Result<(), GuiServerError>)]
78pub struct CloseApp {
79    pub pid: usize,
80}
81
82impl FromScalar<1> for CloseApp {
83    fn from_scalar([pid]: [u32; 1]) -> Self { Self { pid: pid as usize } }
84}
85
86impl AsScalar<1> for CloseApp {
87    fn as_scalar(&self) -> [u32; 1] { [self.pid as u32] }
88}
89
90#[derive(Debug, server::Message)]
91pub struct AnimateNextFrame {
92    pub animation_kind: NextFrameAnimationKind,
93}
94
95#[derive(Debug, Copy, Clone, Default, server::Message)]
96pub struct UpdateKioskPolicy {
97    pub auto_lock_enabled: Option<bool>,
98    pub control_center_enabled: Option<bool>,
99    pub home_button_enabled: Option<bool>,
100    pub power_button_enabled: Option<bool>,
101}
102
103impl UpdateKioskPolicy {
104    const AUTO_LOCK: u32 = 1 << 0;
105    const CONTROL_CENTER: u32 = 1 << 1;
106    const HOME_BUTTON: u32 = 1 << 2;
107    const POWER_BUTTON: u32 = 1 << 3;
108
109    pub fn all(enabled: bool) -> Self {
110        Self::default()
111            .set_auto_lock(enabled)
112            .set_control_center(enabled)
113            .set_home_button(enabled)
114            .set_power_button(enabled)
115    }
116
117    #[inline]
118    pub fn set_auto_lock(mut self, enabled: bool) -> Self {
119        self.auto_lock_enabled = Some(enabled);
120        self
121    }
122
123    #[inline]
124    pub fn set_control_center(mut self, enabled: bool) -> Self {
125        self.control_center_enabled = Some(enabled);
126        self
127    }
128
129    #[inline]
130    pub fn set_home_button(mut self, enabled: bool) -> Self {
131        self.home_button_enabled = Some(enabled);
132        self
133    }
134
135    #[inline]
136    pub fn set_power_button(mut self, enabled: bool) -> Self {
137        self.power_button_enabled = Some(enabled);
138        self
139    }
140}
141
142impl FromScalar<1> for UpdateKioskPolicy {
143    fn from_scalar([flags]: [u32; 1]) -> Self {
144        let present_flags = flags >> 16;
145        let value_flags = flags & u16::MAX as u32;
146        let read_flag = |flag| (present_flags & flag != 0).then_some(value_flags & flag != 0);
147        Self {
148            auto_lock_enabled: read_flag(Self::AUTO_LOCK),
149            control_center_enabled: read_flag(Self::CONTROL_CENTER),
150            home_button_enabled: read_flag(Self::HOME_BUTTON),
151            power_button_enabled: read_flag(Self::POWER_BUTTON),
152        }
153    }
154}
155
156impl AsScalar<1> for UpdateKioskPolicy {
157    fn as_scalar(&self) -> [u32; 1] {
158        let mut present_flags = 0;
159        let mut value_flags = 0;
160        for (flag, value) in [
161            (Self::AUTO_LOCK, self.auto_lock_enabled),
162            (Self::CONTROL_CENTER, self.control_center_enabled),
163            (Self::HOME_BUTTON, self.home_button_enabled),
164            (Self::POWER_BUTTON, self.power_button_enabled),
165        ] {
166            if let Some(enabled) = value {
167                present_flags |= flag;
168                if enabled {
169                    value_flags |= flag;
170                }
171            }
172        }
173        [(present_flags << 16) | value_flags]
174    }
175}
176
177impl FromScalar<1> for AnimateNextFrame {
178    fn from_scalar([animation_kind]: [u32; 1]) -> Self {
179        Self { animation_kind: NextFrameAnimationKind::from_u32(animation_kind).unwrap_or_default() }
180    }
181}
182
183impl AsScalar<1> for AnimateNextFrame {
184    fn as_scalar(&self) -> [u32; 1] { [self.animation_kind as u32] }
185}